import java.util.Scanner;

class Main{
       public static void main(String[] args){
    	Scanner scan = new Scanner(System.in);
    	int r = scan.nextInt();
    	int c = scan.nextInt();
    	int[][] num = new int[r + 1][c + 1];
    	
    	for(int i = 0; i < r; i++){
    		for(int j = 0; j < c; j++){
        		num[i][j] = scan.nextInt();
        	}
    	}
    	
    	for(int i = 0; i < r; i++){
    		for(int j = 0; j < c; j++){
    			num[i][c] += num[i][j];
    		}
    	}
    	for(int j = 0; j < c + 1; j++){
    		for(int i = 0; i < r; i++){
    			num[r][j] += num[i][j];
    		}
    	}
    	
    	for(int i = 0; i < r + 1; i++){
    		for(int j = 0; j < c + 1; j++){
    			if(j != c){
    				System.out.print(num[i][j] + " ");
    			}else{
    				System.out.println(num[i][j]);
    			}
    		}
    	}
    }
} import java.util.Scanner; Scanner java.util class Main{
       public static void main(String[] args){
    	Scanner scan = new Scanner(System.in);
    	int r = scan.nextInt();
    	int c = scan.nextInt();
    	int[][] num = new int[r + 1][c + 1];
    	
    	for(int i = 0; i < r; i++){
    		for(int j = 0; j < c; j++){
        		num[i][j] = scan.nextInt();
        	}
    	}
    	
    	for(int i = 0; i < r; i++){
    		for(int j = 0; j < c; j++){
    			num[i][c] += num[i][j];
    		}
    	}
    	for(int j = 0; j < c + 1; j++){
    		for(int i = 0; i < r; i++){
    			num[r][j] += num[i][j];
    		}
    	}
    	
    	for(int i = 0; i < r + 1; i++){
    		for(int j = 0; j < c + 1; j++){
    			if(j != c){
    				System.out.print(num[i][j] + " ");
    			}else{
    				System.out.println(num[i][j]);
    			}
    		}
    	}
    }
} Main public static void main(String[] args){
    	Scanner scan = new Scanner(System.in);
    	int r = scan.nextInt();
    	int c = scan.nextInt();
    	int[][] num = new int[r + 1][c + 1];
    	
    	for(int i = 0; i < r; i++){
    		for(int j = 0; j < c; j++){
        		num[i][j] = scan.nextInt();
        	}
    	}
    	
    	for(int i = 0; i < r; i++){
    		for(int j = 0; j < c; j++){
    			num[i][c] += num[i][j];
    		}
    	}
    	for(int j = 0; j < c + 1; j++){
    		for(int i = 0; i < r; i++){
    			num[r][j] += num[i][j];
    		}
    	}
    	
    	for(int i = 0; i < r + 1; i++){
    		for(int j = 0; j < c + 1; j++){
    			if(j != c){
    				System.out.print(num[i][j] + " ");
    			}else{
    				System.out.println(num[i][j]);
    			}
    		}
    	}
    } main {
    	Scanner scan = new Scanner(System.in);
    	int r = scan.nextInt();
    	int c = scan.nextInt();
    	int[][] num = new int[r + 1][c + 1];
    	
    	for(int i = 0; i < r; i++){
    		for(int j = 0; j < c; j++){
        		num[i][j] = scan.nextInt();
        	}
    	}
    	
    	for(int i = 0; i < r; i++){
    		for(int j = 0; j < c; j++){
    			num[i][c] += num[i][j];
    		}
    	}
    	for(int j = 0; j < c + 1; j++){
    		for(int i = 0; i < r; i++){
    			num[r][j] += num[i][j];
    		}
    	}
    	
    	for(int i = 0; i < r + 1; i++){
    		for(int j = 0; j < c + 1; j++){
    			if(j != c){
    				System.out.print(num[i][j] + " ");
    			}else{
    				System.out.println(num[i][j]);
    			}
    		}
    	}
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int r = scan.nextInt(); r scan.nextInt() scan.nextInt scan int c = scan.nextInt(); c scan.nextInt() scan.nextInt scan int[][] num = new int[r + 1][c + 1]; num 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++){
        		num[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++){
        		num[i][j] = scan.nextInt();
        	}
    	} {
    		for(int j = 0; j < c; j++){
        		num[i][j] = scan.nextInt();
        	}
    	} for(int j = 0; j < c; j++){
        		num[i][j] = scan.nextInt();
        	} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
        		num[i][j] = scan.nextInt();
        	} {
        		num[i][j] = scan.nextInt();
        	} num[i][j] = scan.nextInt() num[i][j] num[i] num i j scan.nextInt() scan.nextInt scan for(int i = 0; i < r; i++){
    		for(int j = 0; j < c; j++){
    			num[i][c] += num[i][j];
    		}
    	} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
    		for(int j = 0; j < c; j++){
    			num[i][c] += num[i][j];
    		}
    	} {
    		for(int j = 0; j < c; j++){
    			num[i][c] += num[i][j];
    		}
    	} for(int j = 0; j < c; j++){
    			num[i][c] += num[i][j];
    		} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
    			num[i][c] += num[i][j];
    		} {
    			num[i][c] += num[i][j];
    		} num[i][c] += num[i][j] num[i][c] num[i] num i c num[i][j] num[i] num i j for(int j = 0; j < c + 1; j++){
    		for(int i = 0; i < r; i++){
    			num[r][j] += num[i][j];
    		}
    	} int j = 0; int j = 0; j 0 j < c + 1 j c + 1 c 1 j++ j++ j {
    		for(int i = 0; i < r; i++){
    			num[r][j] += num[i][j];
    		}
    	} {
    		for(int i = 0; i < r; i++){
    			num[r][j] += num[i][j];
    		}
    	} for(int i = 0; i < r; i++){
    			num[r][j] += num[i][j];
    		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
    			num[r][j] += num[i][j];
    		} {
    			num[r][j] += num[i][j];
    		} num[r][j] += num[i][j] num[r][j] num[r] num r j num[i][j] num[i] num i j for(int i = 0; i < r + 1; i++){
    		for(int j = 0; j < c + 1; j++){
    			if(j != c){
    				System.out.print(num[i][j] + " ");
    			}else{
    				System.out.println(num[i][j]);
    			}
    		}
    	} 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++){
    			if(j != c){
    				System.out.print(num[i][j] + " ");
    			}else{
    				System.out.println(num[i][j]);
    			}
    		}
    	} {
    		for(int j = 0; j < c + 1; j++){
    			if(j != c){
    				System.out.print(num[i][j] + " ");
    			}else{
    				System.out.println(num[i][j]);
    			}
    		}
    	} for(int j = 0; j < c + 1; j++){
    			if(j != c){
    				System.out.print(num[i][j] + " ");
    			}else{
    				System.out.println(num[i][j]);
    			}
    		} int j = 0; int j = 0; j 0 j < c + 1 j c + 1 c 1 j++ j++ j {
    			if(j != c){
    				System.out.print(num[i][j] + " ");
    			}else{
    				System.out.println(num[i][j]);
    			}
    		} {
    			if(j != c){
    				System.out.print(num[i][j] + " ");
    			}else{
    				System.out.println(num[i][j]);
    			}
    		} if(j != c){
    				System.out.print(num[i][j] + " ");
    			}else{
    				System.out.println(num[i][j]);
    			} j != c j c {
    				System.out.print(num[i][j] + " ");
    			} System.out.print(num[i][j] + " ") System.out.print System.out System System.out num[i][j] + " " num[i][j] num[i] num i j " " {
    				System.out.println(num[i][j]);
    			} System.out.println(num[i][j]) System.out.println System.out System System.out num[i][j] num[i] num i j String[] args args
import java.util.Scanner;

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