import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner scan = null;
		try{
			scan = new Scanner(System.in);
			int r ,c;
			r = scan.nextInt();
			c = scan.nextInt();
			int[][] matrix = new int[r + 1][c + 1];
			for(int i = 0; i < r ;i++){
				for(int j = 0 ; j < c;j++){
					matrix[i][j] = scan.nextInt();
				}
			}
			for(int i = 0 ;i < r;i++){
				int sum = 0;
				for(int j = 0; j < c;j++){
					sum += matrix[i][j];
				}
				matrix[i][c] = sum;
			}
			for(int i = 0 ; i <= c;i++){
				int sum = 0;
				for(int j = 0 ; j < r; j++){
					sum += matrix[j][i];
				}
				matrix[r][i] = sum;
			}
			for(int i = 0 ; i < matrix.length;i++){
				System.out.print(matrix[i][0]);
				for(int j = 1 ; j < matrix[i].length;j++){
					System.out.print(" " + matrix[i][j]);
				}
				System.out.println();
			}
		}finally{
			if(scan !=null)scan.close();
		}
	}
} import java.util.Scanner; Scanner java.util public class Main{
	public static void main(String[] args){
		Scanner scan = null;
		try{
			scan = new Scanner(System.in);
			int r ,c;
			r = scan.nextInt();
			c = scan.nextInt();
			int[][] matrix = new int[r + 1][c + 1];
			for(int i = 0; i < r ;i++){
				for(int j = 0 ; j < c;j++){
					matrix[i][j] = scan.nextInt();
				}
			}
			for(int i = 0 ;i < r;i++){
				int sum = 0;
				for(int j = 0; j < c;j++){
					sum += matrix[i][j];
				}
				matrix[i][c] = sum;
			}
			for(int i = 0 ; i <= c;i++){
				int sum = 0;
				for(int j = 0 ; j < r; j++){
					sum += matrix[j][i];
				}
				matrix[r][i] = sum;
			}
			for(int i = 0 ; i < matrix.length;i++){
				System.out.print(matrix[i][0]);
				for(int j = 1 ; j < matrix[i].length;j++){
					System.out.print(" " + matrix[i][j]);
				}
				System.out.println();
			}
		}finally{
			if(scan !=null)scan.close();
		}
	}
} Main public static void main(String[] args){
		Scanner scan = null;
		try{
			scan = new Scanner(System.in);
			int r ,c;
			r = scan.nextInt();
			c = scan.nextInt();
			int[][] matrix = new int[r + 1][c + 1];
			for(int i = 0; i < r ;i++){
				for(int j = 0 ; j < c;j++){
					matrix[i][j] = scan.nextInt();
				}
			}
			for(int i = 0 ;i < r;i++){
				int sum = 0;
				for(int j = 0; j < c;j++){
					sum += matrix[i][j];
				}
				matrix[i][c] = sum;
			}
			for(int i = 0 ; i <= c;i++){
				int sum = 0;
				for(int j = 0 ; j < r; j++){
					sum += matrix[j][i];
				}
				matrix[r][i] = sum;
			}
			for(int i = 0 ; i < matrix.length;i++){
				System.out.print(matrix[i][0]);
				for(int j = 1 ; j < matrix[i].length;j++){
					System.out.print(" " + matrix[i][j]);
				}
				System.out.println();
			}
		}finally{
			if(scan !=null)scan.close();
		}
	} main {
		Scanner scan = null;
		try{
			scan = new Scanner(System.in);
			int r ,c;
			r = scan.nextInt();
			c = scan.nextInt();
			int[][] matrix = new int[r + 1][c + 1];
			for(int i = 0; i < r ;i++){
				for(int j = 0 ; j < c;j++){
					matrix[i][j] = scan.nextInt();
				}
			}
			for(int i = 0 ;i < r;i++){
				int sum = 0;
				for(int j = 0; j < c;j++){
					sum += matrix[i][j];
				}
				matrix[i][c] = sum;
			}
			for(int i = 0 ; i <= c;i++){
				int sum = 0;
				for(int j = 0 ; j < r; j++){
					sum += matrix[j][i];
				}
				matrix[r][i] = sum;
			}
			for(int i = 0 ; i < matrix.length;i++){
				System.out.print(matrix[i][0]);
				for(int j = 1 ; j < matrix[i].length;j++){
					System.out.print(" " + matrix[i][j]);
				}
				System.out.println();
			}
		}finally{
			if(scan !=null)scan.close();
		}
	} Scanner scan = null; scan null try{
			scan = new Scanner(System.in);
			int r ,c;
			r = scan.nextInt();
			c = scan.nextInt();
			int[][] matrix = new int[r + 1][c + 1];
			for(int i = 0; i < r ;i++){
				for(int j = 0 ; j < c;j++){
					matrix[i][j] = scan.nextInt();
				}
			}
			for(int i = 0 ;i < r;i++){
				int sum = 0;
				for(int j = 0; j < c;j++){
					sum += matrix[i][j];
				}
				matrix[i][c] = sum;
			}
			for(int i = 0 ; i <= c;i++){
				int sum = 0;
				for(int j = 0 ; j < r; j++){
					sum += matrix[j][i];
				}
				matrix[r][i] = sum;
			}
			for(int i = 0 ; i < matrix.length;i++){
				System.out.print(matrix[i][0]);
				for(int j = 1 ; j < matrix[i].length;j++){
					System.out.print(" " + matrix[i][j]);
				}
				System.out.println();
			}
		}finally{
			if(scan !=null)scan.close();
		} {
			if(scan !=null)scan.close();
		} if(scan !=null)scan.close(); scan !=null scan null scan.close() scan.close scan {
			scan = new Scanner(System.in);
			int r ,c;
			r = scan.nextInt();
			c = scan.nextInt();
			int[][] matrix = new int[r + 1][c + 1];
			for(int i = 0; i < r ;i++){
				for(int j = 0 ; j < c;j++){
					matrix[i][j] = scan.nextInt();
				}
			}
			for(int i = 0 ;i < r;i++){
				int sum = 0;
				for(int j = 0; j < c;j++){
					sum += matrix[i][j];
				}
				matrix[i][c] = sum;
			}
			for(int i = 0 ; i <= c;i++){
				int sum = 0;
				for(int j = 0 ; j < r; j++){
					sum += matrix[j][i];
				}
				matrix[r][i] = sum;
			}
			for(int i = 0 ; i < matrix.length;i++){
				System.out.print(matrix[i][0]);
				for(int j = 1 ; j < matrix[i].length;j++){
					System.out.print(" " + matrix[i][j]);
				}
				System.out.println();
			}
		} scan = new Scanner(System.in) scan new Scanner(System.in) int r r c; c r = scan.nextInt() r scan.nextInt() scan.nextInt scan c = scan.nextInt() c scan.nextInt() scan.nextInt scan int[][] matrix = new int[r + 1][c + 1]; matrix new int[r + 1][c + 1] r + 1 r 1 c + 1 c 1 for(int i = 0; i < r ;i++){
				for(int j = 0 ; j < c;j++){
					matrix[i][j] = scan.nextInt();
				}
			} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
				for(int j = 0 ; j < c;j++){
					matrix[i][j] = scan.nextInt();
				}
			} {
				for(int j = 0 ; j < c;j++){
					matrix[i][j] = scan.nextInt();
				}
			} for(int j = 0 ; j < c;j++){
					matrix[i][j] = scan.nextInt();
				} int j = 0 ; int j = 0 ; j 0 j < c j c j++ j++ j {
					matrix[i][j] = scan.nextInt();
				} {
					matrix[i][j] = scan.nextInt();
				} matrix[i][j] = scan.nextInt() matrix[i][j] matrix[i] matrix i j scan.nextInt() scan.nextInt scan for(int i = 0 ;i < r;i++){
				int sum = 0;
				for(int j = 0; j < c;j++){
					sum += matrix[i][j];
				}
				matrix[i][c] = sum;
			} int i = 0 ; int i = 0 ; i 0 i < r i r i++ i++ i {
				int sum = 0;
				for(int j = 0; j < c;j++){
					sum += matrix[i][j];
				}
				matrix[i][c] = sum;
			} {
				int sum = 0;
				for(int j = 0; j < c;j++){
					sum += matrix[i][j];
				}
				matrix[i][c] = sum;
			} int sum = 0; sum 0 for(int j = 0; j < c;j++){
					sum += matrix[i][j];
				} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
					sum += matrix[i][j];
				} {
					sum += matrix[i][j];
				} sum += matrix[i][j] sum matrix[i][j] matrix[i] matrix i j matrix[i][c] = sum matrix[i][c] matrix[i] matrix i c sum for(int i = 0 ; i <= c;i++){
				int sum = 0;
				for(int j = 0 ; j < r; j++){
					sum += matrix[j][i];
				}
				matrix[r][i] = sum;
			} int i = 0 ; int i = 0 ; i 0 i <= c i c i++ i++ i {
				int sum = 0;
				for(int j = 0 ; j < r; j++){
					sum += matrix[j][i];
				}
				matrix[r][i] = sum;
			} {
				int sum = 0;
				for(int j = 0 ; j < r; j++){
					sum += matrix[j][i];
				}
				matrix[r][i] = sum;
			} int sum = 0; sum 0 for(int j = 0 ; j < r; j++){
					sum += matrix[j][i];
				} int j = 0 ; int j = 0 ; j 0 j < r j r j++ j++ j {
					sum += matrix[j][i];
				} {
					sum += matrix[j][i];
				} sum += matrix[j][i] sum matrix[j][i] matrix[j] matrix j i matrix[r][i] = sum matrix[r][i] matrix[r] matrix r i sum for(int i = 0 ; i < matrix.length;i++){
				System.out.print(matrix[i][0]);
				for(int j = 1 ; j < matrix[i].length;j++){
					System.out.print(" " + matrix[i][j]);
				}
				System.out.println();
			} int i = 0 ; int i = 0 ; i 0 i < matrix.length i matrix.length matrix matrix.length i++ i++ i {
				System.out.print(matrix[i][0]);
				for(int j = 1 ; j < matrix[i].length;j++){
					System.out.print(" " + matrix[i][j]);
				}
				System.out.println();
			} {
				System.out.print(matrix[i][0]);
				for(int j = 1 ; j < matrix[i].length;j++){
					System.out.print(" " + matrix[i][j]);
				}
				System.out.println();
			} System.out.print(matrix[i][0]) System.out.print System.out System System.out matrix[i][0] matrix[i] matrix i 0 for(int j = 1 ; j < matrix[i].length;j++){
					System.out.print(" " + matrix[i][j]);
				} int j = 1 ; int j = 1 ; j 1 j < matrix[i].length j matrix[i].length matrix[i] matrix i matrix[i].length j++ j++ j {
					System.out.print(" " + matrix[i][j]);
				} {
					System.out.print(" " + matrix[i][j]);
				} System.out.print(" " + matrix[i][j]) System.out.print System.out System System.out " " + matrix[i][j] " " matrix[i][j] matrix[i] matrix i j System.out.println() System.out.println System.out System System.out String[] args args public class Main{
	public static void main(String[] args){
		Scanner scan = null;
		try{
			scan = new Scanner(System.in);
			int r ,c;
			r = scan.nextInt();
			c = scan.nextInt();
			int[][] matrix = new int[r + 1][c + 1];
			for(int i = 0; i < r ;i++){
				for(int j = 0 ; j < c;j++){
					matrix[i][j] = scan.nextInt();
				}
			}
			for(int i = 0 ;i < r;i++){
				int sum = 0;
				for(int j = 0; j < c;j++){
					sum += matrix[i][j];
				}
				matrix[i][c] = sum;
			}
			for(int i = 0 ; i <= c;i++){
				int sum = 0;
				for(int j = 0 ; j < r; j++){
					sum += matrix[j][i];
				}
				matrix[r][i] = sum;
			}
			for(int i = 0 ; i < matrix.length;i++){
				System.out.print(matrix[i][0]);
				for(int j = 1 ; j < matrix[i].length;j++){
					System.out.print(" " + matrix[i][j]);
				}
				System.out.println();
			}
		}finally{
			if(scan !=null)scan.close();
		}
	}
} public class Main{
	public static void main(String[] args){
		Scanner scan = null;
		try{
			scan = new Scanner(System.in);
			int r ,c;
			r = scan.nextInt();
			c = scan.nextInt();
			int[][] matrix = new int[r + 1][c + 1];
			for(int i = 0; i < r ;i++){
				for(int j = 0 ; j < c;j++){
					matrix[i][j] = scan.nextInt();
				}
			}
			for(int i = 0 ;i < r;i++){
				int sum = 0;
				for(int j = 0; j < c;j++){
					sum += matrix[i][j];
				}
				matrix[i][c] = sum;
			}
			for(int i = 0 ; i <= c;i++){
				int sum = 0;
				for(int j = 0 ; j < r; j++){
					sum += matrix[j][i];
				}
				matrix[r][i] = sum;
			}
			for(int i = 0 ; i < matrix.length;i++){
				System.out.print(matrix[i][0]);
				for(int j = 1 ; j < matrix[i].length;j++){
					System.out.print(" " + matrix[i][j]);
				}
				System.out.println();
			}
		}finally{
			if(scan !=null)scan.close();
		}
	}
} Main
import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner scan = null;
		try{
			scan = new Scanner(System.in);
			int r ,c;
			r = scan.nextInt();
			c = scan.nextInt();
			int[][] matrix = new int[r + 1][c + 1];
			for(int i = 0; i < r ;i++){
				for(int j = 0 ; j < c;j++){
					matrix[i][j] = scan.nextInt();
				}
			}
			for(int i = 0 ;i < r;i++){
				int sum = 0;
				for(int j = 0; j < c;j++){
					sum += matrix[i][j];
				}
				matrix[i][c] = sum;
			}
			for(int i = 0 ; i <= c;i++){
				int sum = 0;
				for(int j = 0 ; j < r; j++){
					sum += matrix[j][i];
				}
				matrix[r][i] = sum;
			}
			for(int i = 0 ; i < matrix.length;i++){
				System.out.print(matrix[i][0]);
				for(int j = 1 ; j < matrix[i].length;j++){
					System.out.print(" " + matrix[i][j]);
				}
				System.out.println();
			}
		}finally{
			if(scan !=null)scan.close();
		}
	}
}