import java.util.Scanner;

class Main{
	public static void main(String[] args){
    	int r,c;
    	int total= 0;
    	Scanner sc = new Scanner(System.in);
    	r = sc.nextInt();
        c = sc.nextInt();
        int [][] a = new int [r+1][c+1];

    	for(int i = 0; i < r; i++){
			for(int j = 0; j < c+1; j++){
				if(j != c){
					a[i][j] = sc.nextInt();
					a[r][j] += a[i][j];
	    			System.out.print(a[i][j]+" ");
	    			total += a[i][j];
				}
				else{
					a[i][j] = total;
					System.out.println(a[i][j]);
					a[r][j] += a[i][j];
				}
			}
			total = 0;
    	}
    	sc.close();

    	for(int i = 0; i < c+1; i++){
    		if(i != c){
    			System.out.print(a[r][i]+" ");
    		}
    		else{
    			System.out.println(a[r][i]);
    		}
    	}
    }
} import java.util.Scanner; Scanner java.util class Main{
	public static void main(String[] args){
    	int r,c;
    	int total= 0;
    	Scanner sc = new Scanner(System.in);
    	r = sc.nextInt();
        c = sc.nextInt();
        int [][] a = new int [r+1][c+1];

    	for(int i = 0; i < r; i++){
			for(int j = 0; j < c+1; j++){
				if(j != c){
					a[i][j] = sc.nextInt();
					a[r][j] += a[i][j];
	    			System.out.print(a[i][j]+" ");
	    			total += a[i][j];
				}
				else{
					a[i][j] = total;
					System.out.println(a[i][j]);
					a[r][j] += a[i][j];
				}
			}
			total = 0;
    	}
    	sc.close();

    	for(int i = 0; i < c+1; i++){
    		if(i != c){
    			System.out.print(a[r][i]+" ");
    		}
    		else{
    			System.out.println(a[r][i]);
    		}
    	}
    }
} Main public static void main(String[] args){
    	int r,c;
    	int total= 0;
    	Scanner sc = new Scanner(System.in);
    	r = sc.nextInt();
        c = sc.nextInt();
        int [][] a = new int [r+1][c+1];

    	for(int i = 0; i < r; i++){
			for(int j = 0; j < c+1; j++){
				if(j != c){
					a[i][j] = sc.nextInt();
					a[r][j] += a[i][j];
	    			System.out.print(a[i][j]+" ");
	    			total += a[i][j];
				}
				else{
					a[i][j] = total;
					System.out.println(a[i][j]);
					a[r][j] += a[i][j];
				}
			}
			total = 0;
    	}
    	sc.close();

    	for(int i = 0; i < c+1; i++){
    		if(i != c){
    			System.out.print(a[r][i]+" ");
    		}
    		else{
    			System.out.println(a[r][i]);
    		}
    	}
    } main {
    	int r,c;
    	int total= 0;
    	Scanner sc = new Scanner(System.in);
    	r = sc.nextInt();
        c = sc.nextInt();
        int [][] a = new int [r+1][c+1];

    	for(int i = 0; i < r; i++){
			for(int j = 0; j < c+1; j++){
				if(j != c){
					a[i][j] = sc.nextInt();
					a[r][j] += a[i][j];
	    			System.out.print(a[i][j]+" ");
	    			total += a[i][j];
				}
				else{
					a[i][j] = total;
					System.out.println(a[i][j]);
					a[r][j] += a[i][j];
				}
			}
			total = 0;
    	}
    	sc.close();

    	for(int i = 0; i < c+1; i++){
    		if(i != c){
    			System.out.print(a[r][i]+" ");
    		}
    		else{
    			System.out.println(a[r][i]);
    		}
    	}
    } int r r c; c int total= 0; total 0 Scanner sc = new Scanner(System.in); sc new Scanner(System.in) r = sc.nextInt() r sc.nextInt() sc.nextInt sc c = sc.nextInt() c sc.nextInt() sc.nextInt sc int [][] a = new int [r+1][c+1]; a 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+1; j++){
				if(j != c){
					a[i][j] = sc.nextInt();
					a[r][j] += a[i][j];
	    			System.out.print(a[i][j]+" ");
	    			total += a[i][j];
				}
				else{
					a[i][j] = total;
					System.out.println(a[i][j]);
					a[r][j] += a[i][j];
				}
			}
			total = 0;
    	} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			for(int j = 0; j < c+1; j++){
				if(j != c){
					a[i][j] = sc.nextInt();
					a[r][j] += a[i][j];
	    			System.out.print(a[i][j]+" ");
	    			total += a[i][j];
				}
				else{
					a[i][j] = total;
					System.out.println(a[i][j]);
					a[r][j] += a[i][j];
				}
			}
			total = 0;
    	} {
			for(int j = 0; j < c+1; j++){
				if(j != c){
					a[i][j] = sc.nextInt();
					a[r][j] += a[i][j];
	    			System.out.print(a[i][j]+" ");
	    			total += a[i][j];
				}
				else{
					a[i][j] = total;
					System.out.println(a[i][j]);
					a[r][j] += a[i][j];
				}
			}
			total = 0;
    	} for(int j = 0; j < c+1; j++){
				if(j != c){
					a[i][j] = sc.nextInt();
					a[r][j] += a[i][j];
	    			System.out.print(a[i][j]+" ");
	    			total += a[i][j];
				}
				else{
					a[i][j] = total;
					System.out.println(a[i][j]);
					a[r][j] += a[i][j];
				}
			} int j = 0; int j = 0; j 0 j < c+1 j c+1 c 1 j++ j++ j {
				if(j != c){
					a[i][j] = sc.nextInt();
					a[r][j] += a[i][j];
	    			System.out.print(a[i][j]+" ");
	    			total += a[i][j];
				}
				else{
					a[i][j] = total;
					System.out.println(a[i][j]);
					a[r][j] += a[i][j];
				}
			} {
				if(j != c){
					a[i][j] = sc.nextInt();
					a[r][j] += a[i][j];
	    			System.out.print(a[i][j]+" ");
	    			total += a[i][j];
				}
				else{
					a[i][j] = total;
					System.out.println(a[i][j]);
					a[r][j] += a[i][j];
				}
			} if(j != c){
					a[i][j] = sc.nextInt();
					a[r][j] += a[i][j];
	    			System.out.print(a[i][j]+" ");
	    			total += a[i][j];
				}
				else{
					a[i][j] = total;
					System.out.println(a[i][j]);
					a[r][j] += a[i][j];
				} j != c j c {
					a[i][j] = sc.nextInt();
					a[r][j] += a[i][j];
	    			System.out.print(a[i][j]+" ");
	    			total += a[i][j];
				} a[i][j] = sc.nextInt() a[i][j] a[i] a i j sc.nextInt() sc.nextInt sc a[r][j] += a[i][j] a[r][j] a[r] a r j a[i][j] a[i] a i j System.out.print(a[i][j]+" ") System.out.print System.out System System.out a[i][j]+" " a[i][j] a[i] a i j " " total += a[i][j] total a[i][j] a[i] a i j {
					a[i][j] = total;
					System.out.println(a[i][j]);
					a[r][j] += a[i][j];
				} a[i][j] = total a[i][j] a[i] a i j total System.out.println(a[i][j]) System.out.println System.out System System.out a[i][j] a[i] a i j a[r][j] += a[i][j] a[r][j] a[r] a r j a[i][j] a[i] a i j total = 0 total 0 sc.close() sc.close sc for(int i = 0; i < c+1; i++){
    		if(i != c){
    			System.out.print(a[r][i]+" ");
    		}
    		else{
    			System.out.println(a[r][i]);
    		}
    	} int i = 0; int i = 0; i 0 i < c+1 i c+1 c 1 i++ i++ i {
    		if(i != c){
    			System.out.print(a[r][i]+" ");
    		}
    		else{
    			System.out.println(a[r][i]);
    		}
    	} {
    		if(i != c){
    			System.out.print(a[r][i]+" ");
    		}
    		else{
    			System.out.println(a[r][i]);
    		}
    	} if(i != c){
    			System.out.print(a[r][i]+" ");
    		}
    		else{
    			System.out.println(a[r][i]);
    		} i != c i c {
    			System.out.print(a[r][i]+" ");
    		} System.out.print(a[r][i]+" ") System.out.print System.out System System.out a[r][i]+" " a[r][i] a[r] a r i " " {
    			System.out.println(a[r][i]);
    		} System.out.println(a[r][i]) System.out.println System.out System System.out a[r][i] a[r] a r i String[] args args
import java.util.Scanner;

class Main{
	public static void main(String[] args){
    	int r,c;
    	int total= 0;
    	Scanner sc = new Scanner(System.in);
    	r = sc.nextInt();
        c = sc.nextInt();
        int [][] a = new int [r+1][c+1];

    	for(int i = 0; i < r; i++){
			for(int j = 0; j < c+1; j++){
				if(j != c){
					a[i][j] = sc.nextInt();
					a[r][j] += a[i][j];
	    			System.out.print(a[i][j]+" ");
	    			total += a[i][j];
				}
				else{
					a[i][j] = total;
					System.out.println(a[i][j]);
					a[r][j] += a[i][j];
				}
			}
			total = 0;
    	}
    	sc.close();

    	for(int i = 0; i < c+1; i++){
    		if(i != c){
    			System.out.print(a[r][i]+" ");
    		}
    		else{
    			System.out.println(a[r][i]);
    		}
    	}
    }
}