import java.util.Scanner;

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

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