import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int r = in.nextInt();
		int c = in.nextInt();
		int[][] ary = new int[r+1][c+1];
		
		//Insert the given values
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				ary[i][j] = in.nextInt();
			}
		}
		//Calculate the total values
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				ary[i][c] += ary[i][j];
				ary[r][j] += ary[i][j];
				ary[r][c] += ary[i][j];
			}
		}
		//Display the matrix
		for(int i=0; i<r+1; i++){
			for(int j=0; j<c+1; j++){
				System.out.print(ary[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		in.close();
	}
} import java.util.Scanner; Scanner java.util public class Main{
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int r = in.nextInt();
		int c = in.nextInt();
		int[][] ary = new int[r+1][c+1];
		
		//Insert the given values
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				ary[i][j] = in.nextInt();
			}
		}
		//Calculate the total values
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				ary[i][c] += ary[i][j];
				ary[r][j] += ary[i][j];
				ary[r][c] += ary[i][j];
			}
		}
		//Display the matrix
		for(int i=0; i<r+1; i++){
			for(int j=0; j<c+1; j++){
				System.out.print(ary[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		in.close();
	}
} Main public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int r = in.nextInt();
		int c = in.nextInt();
		int[][] ary = new int[r+1][c+1];
		
		//Insert the given values
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				ary[i][j] = in.nextInt();
			}
		}
		//Calculate the total values
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				ary[i][c] += ary[i][j];
				ary[r][j] += ary[i][j];
				ary[r][c] += ary[i][j];
			}
		}
		//Display the matrix
		for(int i=0; i<r+1; i++){
			for(int j=0; j<c+1; j++){
				System.out.print(ary[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		in.close();
	} main {
		Scanner in = new Scanner(System.in);
		int r = in.nextInt();
		int c = in.nextInt();
		int[][] ary = new int[r+1][c+1];
		
		//Insert the given values
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				ary[i][j] = in.nextInt();
			}
		}
		//Calculate the total values
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				ary[i][c] += ary[i][j];
				ary[r][j] += ary[i][j];
				ary[r][c] += ary[i][j];
			}
		}
		//Display the matrix
		for(int i=0; i<r+1; i++){
			for(int j=0; j<c+1; j++){
				System.out.print(ary[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		in.close();
	} Scanner in = new Scanner(System.in); in new Scanner(System.in) int r = in.nextInt(); r in.nextInt() in.nextInt in int c = in.nextInt(); c in.nextInt() in.nextInt in int[][] ary = new int[r+1][c+1]; ary 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++){
				ary[i][j] = in.nextInt();
			}
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			for(int j=0; j<c; j++){
				ary[i][j] = in.nextInt();
			}
		} {
			for(int j=0; j<c; j++){
				ary[i][j] = in.nextInt();
			}
		} for(int j=0; j<c; j++){
				ary[i][j] = in.nextInt();
			} int j=0; int j=0; j 0 j<c j c j++ j++ j {
				ary[i][j] = in.nextInt();
			} {
				ary[i][j] = in.nextInt();
			} ary[i][j] = in.nextInt() ary[i][j] ary[i] ary i j in.nextInt() in.nextInt in for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				ary[i][c] += ary[i][j];
				ary[r][j] += ary[i][j];
				ary[r][c] += ary[i][j];
			}
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			for(int j=0; j<c; j++){
				ary[i][c] += ary[i][j];
				ary[r][j] += ary[i][j];
				ary[r][c] += ary[i][j];
			}
		} {
			for(int j=0; j<c; j++){
				ary[i][c] += ary[i][j];
				ary[r][j] += ary[i][j];
				ary[r][c] += ary[i][j];
			}
		} for(int j=0; j<c; j++){
				ary[i][c] += ary[i][j];
				ary[r][j] += ary[i][j];
				ary[r][c] += ary[i][j];
			} int j=0; int j=0; j 0 j<c j c j++ j++ j {
				ary[i][c] += ary[i][j];
				ary[r][j] += ary[i][j];
				ary[r][c] += ary[i][j];
			} {
				ary[i][c] += ary[i][j];
				ary[r][j] += ary[i][j];
				ary[r][c] += ary[i][j];
			} ary[i][c] += ary[i][j] ary[i][c] ary[i] ary i c ary[i][j] ary[i] ary i j ary[r][j] += ary[i][j] ary[r][j] ary[r] ary r j ary[i][j] ary[i] ary i j ary[r][c] += ary[i][j] ary[r][c] ary[r] ary r c ary[i][j] ary[i] ary i j for(int i=0; i<r+1; i++){
			for(int j=0; j<c+1; j++){
				System.out.print(ary[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		} int i=0; int i=0; i 0 i<r+1 i r+1 r 1 i++ i++ i {
			for(int j=0; j<c+1; j++){
				System.out.print(ary[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		} {
			for(int j=0; j<c+1; j++){
				System.out.print(ary[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		} for(int j=0; j<c+1; j++){
				System.out.print(ary[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			} int j=0; int j=0; j 0 j<c+1 j c+1 c 1 j++ j++ j {
				System.out.print(ary[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			} {
				System.out.print(ary[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			} System.out.print(ary[i][j]) System.out.print System.out System System.out ary[i][j] ary[i] ary i j if(j != c){
					System.out.print(" ");
				} j != c j c {
					System.out.print(" ");
				} System.out.print(" ") System.out.print System.out System System.out " " System.out.println() System.out.println System.out System System.out in.close() in.close in String[] args args public class Main{
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int r = in.nextInt();
		int c = in.nextInt();
		int[][] ary = new int[r+1][c+1];
		
		//Insert the given values
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				ary[i][j] = in.nextInt();
			}
		}
		//Calculate the total values
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				ary[i][c] += ary[i][j];
				ary[r][j] += ary[i][j];
				ary[r][c] += ary[i][j];
			}
		}
		//Display the matrix
		for(int i=0; i<r+1; i++){
			for(int j=0; j<c+1; j++){
				System.out.print(ary[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		in.close();
	}
} public class Main{
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int r = in.nextInt();
		int c = in.nextInt();
		int[][] ary = new int[r+1][c+1];
		
		//Insert the given values
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				ary[i][j] = in.nextInt();
			}
		}
		//Calculate the total values
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				ary[i][c] += ary[i][j];
				ary[r][j] += ary[i][j];
				ary[r][c] += ary[i][j];
			}
		}
		//Display the matrix
		for(int i=0; i<r+1; i++){
			for(int j=0; j<c+1; j++){
				System.out.print(ary[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		in.close();
	}
} Main
import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int r = in.nextInt();
		int c = in.nextInt();
		int[][] ary = new int[r+1][c+1];
		
		//Insert the given values
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				ary[i][j] = in.nextInt();
			}
		}
		//Calculate the total values
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				ary[i][c] += ary[i][j];
				ary[r][j] += ary[i][j];
				ary[r][c] += ary[i][j];
			}
		}
		//Display the matrix
		for(int i=0; i<r+1; i++){
			for(int j=0; j<c+1; j++){
				System.out.print(ary[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		in.close();
	}
}