import java.io.IOException;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) throws IOException{

		Scanner sc = new Scanner(System.in);

		int x = sc.nextInt();
		int y = sc.nextInt();

		int[][] sheet = new int[x+1][y+1];
		int[] row = new int[x];
		int[] col = new int[y];
		int total = 0;

		for(int i=0;i<x+1;i++) {
			for(int j=0;j<y+1;j++) {
				if(i!=x && j!=y){
					sheet[i][j] = sc.nextInt();
					total += sheet[i][j];
					row[i] += sheet[i][j];
					col[j] += sheet[i][j];
				}else if(i!=x && j==y) {
					sheet[i][j] = row[i];
				}else if(i==x && j!=y) {
					sheet[i][j] = col[j];
				}else if(i==x && j==y) {
					sheet[i][j] = total;
				}
			}
		}

		for(int i=0;i<x+1;i++) {
			for(int j=0;j<y+1;j++) {
				System.out.print(sheet[i][j]);
				if(j!=y) {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		sc.close();
	}
} import java.io.IOException; IOException java.io import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) throws IOException{

		Scanner sc = new Scanner(System.in);

		int x = sc.nextInt();
		int y = sc.nextInt();

		int[][] sheet = new int[x+1][y+1];
		int[] row = new int[x];
		int[] col = new int[y];
		int total = 0;

		for(int i=0;i<x+1;i++) {
			for(int j=0;j<y+1;j++) {
				if(i!=x && j!=y){
					sheet[i][j] = sc.nextInt();
					total += sheet[i][j];
					row[i] += sheet[i][j];
					col[j] += sheet[i][j];
				}else if(i!=x && j==y) {
					sheet[i][j] = row[i];
				}else if(i==x && j!=y) {
					sheet[i][j] = col[j];
				}else if(i==x && j==y) {
					sheet[i][j] = total;
				}
			}
		}

		for(int i=0;i<x+1;i++) {
			for(int j=0;j<y+1;j++) {
				System.out.print(sheet[i][j]);
				if(j!=y) {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		sc.close();
	}
} Main public static void main(String[] args) throws IOException{

		Scanner sc = new Scanner(System.in);

		int x = sc.nextInt();
		int y = sc.nextInt();

		int[][] sheet = new int[x+1][y+1];
		int[] row = new int[x];
		int[] col = new int[y];
		int total = 0;

		for(int i=0;i<x+1;i++) {
			for(int j=0;j<y+1;j++) {
				if(i!=x && j!=y){
					sheet[i][j] = sc.nextInt();
					total += sheet[i][j];
					row[i] += sheet[i][j];
					col[j] += sheet[i][j];
				}else if(i!=x && j==y) {
					sheet[i][j] = row[i];
				}else if(i==x && j!=y) {
					sheet[i][j] = col[j];
				}else if(i==x && j==y) {
					sheet[i][j] = total;
				}
			}
		}

		for(int i=0;i<x+1;i++) {
			for(int j=0;j<y+1;j++) {
				System.out.print(sheet[i][j]);
				if(j!=y) {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		sc.close();
	} main {

		Scanner sc = new Scanner(System.in);

		int x = sc.nextInt();
		int y = sc.nextInt();

		int[][] sheet = new int[x+1][y+1];
		int[] row = new int[x];
		int[] col = new int[y];
		int total = 0;

		for(int i=0;i<x+1;i++) {
			for(int j=0;j<y+1;j++) {
				if(i!=x && j!=y){
					sheet[i][j] = sc.nextInt();
					total += sheet[i][j];
					row[i] += sheet[i][j];
					col[j] += sheet[i][j];
				}else if(i!=x && j==y) {
					sheet[i][j] = row[i];
				}else if(i==x && j!=y) {
					sheet[i][j] = col[j];
				}else if(i==x && j==y) {
					sheet[i][j] = total;
				}
			}
		}

		for(int i=0;i<x+1;i++) {
			for(int j=0;j<y+1;j++) {
				System.out.print(sheet[i][j]);
				if(j!=y) {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		sc.close();
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int x = sc.nextInt(); x sc.nextInt() sc.nextInt sc int y = sc.nextInt(); y sc.nextInt() sc.nextInt sc int[][] sheet = new int[x+1][y+1]; sheet new int[x+1][y+1] x+1 x 1 y+1 y 1 int[] row = new int[x]; row new int[x] x int[] col = new int[y]; col new int[y] y int total = 0; total 0 for(int i=0;i<x+1;i++) {
			for(int j=0;j<y+1;j++) {
				if(i!=x && j!=y){
					sheet[i][j] = sc.nextInt();
					total += sheet[i][j];
					row[i] += sheet[i][j];
					col[j] += sheet[i][j];
				}else if(i!=x && j==y) {
					sheet[i][j] = row[i];
				}else if(i==x && j!=y) {
					sheet[i][j] = col[j];
				}else if(i==x && j==y) {
					sheet[i][j] = total;
				}
			}
		} int i=0; int i=0; i 0 i<x+1 i x+1 x 1 i++ i++ i {
			for(int j=0;j<y+1;j++) {
				if(i!=x && j!=y){
					sheet[i][j] = sc.nextInt();
					total += sheet[i][j];
					row[i] += sheet[i][j];
					col[j] += sheet[i][j];
				}else if(i!=x && j==y) {
					sheet[i][j] = row[i];
				}else if(i==x && j!=y) {
					sheet[i][j] = col[j];
				}else if(i==x && j==y) {
					sheet[i][j] = total;
				}
			}
		} {
			for(int j=0;j<y+1;j++) {
				if(i!=x && j!=y){
					sheet[i][j] = sc.nextInt();
					total += sheet[i][j];
					row[i] += sheet[i][j];
					col[j] += sheet[i][j];
				}else if(i!=x && j==y) {
					sheet[i][j] = row[i];
				}else if(i==x && j!=y) {
					sheet[i][j] = col[j];
				}else if(i==x && j==y) {
					sheet[i][j] = total;
				}
			}
		} for(int j=0;j<y+1;j++) {
				if(i!=x && j!=y){
					sheet[i][j] = sc.nextInt();
					total += sheet[i][j];
					row[i] += sheet[i][j];
					col[j] += sheet[i][j];
				}else if(i!=x && j==y) {
					sheet[i][j] = row[i];
				}else if(i==x && j!=y) {
					sheet[i][j] = col[j];
				}else if(i==x && j==y) {
					sheet[i][j] = total;
				}
			} int j=0; int j=0; j 0 j<y+1 j y+1 y 1 j++ j++ j {
				if(i!=x && j!=y){
					sheet[i][j] = sc.nextInt();
					total += sheet[i][j];
					row[i] += sheet[i][j];
					col[j] += sheet[i][j];
				}else if(i!=x && j==y) {
					sheet[i][j] = row[i];
				}else if(i==x && j!=y) {
					sheet[i][j] = col[j];
				}else if(i==x && j==y) {
					sheet[i][j] = total;
				}
			} {
				if(i!=x && j!=y){
					sheet[i][j] = sc.nextInt();
					total += sheet[i][j];
					row[i] += sheet[i][j];
					col[j] += sheet[i][j];
				}else if(i!=x && j==y) {
					sheet[i][j] = row[i];
				}else if(i==x && j!=y) {
					sheet[i][j] = col[j];
				}else if(i==x && j==y) {
					sheet[i][j] = total;
				}
			} if(i!=x && j!=y){
					sheet[i][j] = sc.nextInt();
					total += sheet[i][j];
					row[i] += sheet[i][j];
					col[j] += sheet[i][j];
				}else if(i!=x && j==y) {
					sheet[i][j] = row[i];
				}else if(i==x && j!=y) {
					sheet[i][j] = col[j];
				}else if(i==x && j==y) {
					sheet[i][j] = total;
				} i!=x && j!=y i!=x i x j!=y j y {
					sheet[i][j] = sc.nextInt();
					total += sheet[i][j];
					row[i] += sheet[i][j];
					col[j] += sheet[i][j];
				} sheet[i][j] = sc.nextInt() sheet[i][j] sheet[i] sheet i j sc.nextInt() sc.nextInt sc total += sheet[i][j] total sheet[i][j] sheet[i] sheet i j row[i] += sheet[i][j] row[i] row i sheet[i][j] sheet[i] sheet i j col[j] += sheet[i][j] col[j] col j sheet[i][j] sheet[i] sheet i j if(i!=x && j==y) {
					sheet[i][j] = row[i];
				}else if(i==x && j!=y) {
					sheet[i][j] = col[j];
				}else if(i==x && j==y) {
					sheet[i][j] = total;
				} i!=x && j==y i!=x i x j==y j y {
					sheet[i][j] = row[i];
				} sheet[i][j] = row[i] sheet[i][j] sheet[i] sheet i j row[i] row i if(i==x && j!=y) {
					sheet[i][j] = col[j];
				}else if(i==x && j==y) {
					sheet[i][j] = total;
				} i==x && j!=y i==x i x j!=y j y {
					sheet[i][j] = col[j];
				} sheet[i][j] = col[j] sheet[i][j] sheet[i] sheet i j col[j] col j if(i==x && j==y) {
					sheet[i][j] = total;
				} i==x && j==y i==x i x j==y j y {
					sheet[i][j] = total;
				} sheet[i][j] = total sheet[i][j] sheet[i] sheet i j total for(int i=0;i<x+1;i++) {
			for(int j=0;j<y+1;j++) {
				System.out.print(sheet[i][j]);
				if(j!=y) {
					System.out.print(" ");
				}
			}
			System.out.println();
		} int i=0; int i=0; i 0 i<x+1 i x+1 x 1 i++ i++ i {
			for(int j=0;j<y+1;j++) {
				System.out.print(sheet[i][j]);
				if(j!=y) {
					System.out.print(" ");
				}
			}
			System.out.println();
		} {
			for(int j=0;j<y+1;j++) {
				System.out.print(sheet[i][j]);
				if(j!=y) {
					System.out.print(" ");
				}
			}
			System.out.println();
		} for(int j=0;j<y+1;j++) {
				System.out.print(sheet[i][j]);
				if(j!=y) {
					System.out.print(" ");
				}
			} int j=0; int j=0; j 0 j<y+1 j y+1 y 1 j++ j++ j {
				System.out.print(sheet[i][j]);
				if(j!=y) {
					System.out.print(" ");
				}
			} {
				System.out.print(sheet[i][j]);
				if(j!=y) {
					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!=y) {
					System.out.print(" ");
				} j!=y j y {
					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 sc.close() sc.close sc String[] args args public class Main {
	public static void main(String[] args) throws IOException{

		Scanner sc = new Scanner(System.in);

		int x = sc.nextInt();
		int y = sc.nextInt();

		int[][] sheet = new int[x+1][y+1];
		int[] row = new int[x];
		int[] col = new int[y];
		int total = 0;

		for(int i=0;i<x+1;i++) {
			for(int j=0;j<y+1;j++) {
				if(i!=x && j!=y){
					sheet[i][j] = sc.nextInt();
					total += sheet[i][j];
					row[i] += sheet[i][j];
					col[j] += sheet[i][j];
				}else if(i!=x && j==y) {
					sheet[i][j] = row[i];
				}else if(i==x && j!=y) {
					sheet[i][j] = col[j];
				}else if(i==x && j==y) {
					sheet[i][j] = total;
				}
			}
		}

		for(int i=0;i<x+1;i++) {
			for(int j=0;j<y+1;j++) {
				System.out.print(sheet[i][j]);
				if(j!=y) {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		sc.close();
	}
} public class Main {
	public static void main(String[] args) throws IOException{

		Scanner sc = new Scanner(System.in);

		int x = sc.nextInt();
		int y = sc.nextInt();

		int[][] sheet = new int[x+1][y+1];
		int[] row = new int[x];
		int[] col = new int[y];
		int total = 0;

		for(int i=0;i<x+1;i++) {
			for(int j=0;j<y+1;j++) {
				if(i!=x && j!=y){
					sheet[i][j] = sc.nextInt();
					total += sheet[i][j];
					row[i] += sheet[i][j];
					col[j] += sheet[i][j];
				}else if(i!=x && j==y) {
					sheet[i][j] = row[i];
				}else if(i==x && j!=y) {
					sheet[i][j] = col[j];
				}else if(i==x && j==y) {
					sheet[i][j] = total;
				}
			}
		}

		for(int i=0;i<x+1;i++) {
			for(int j=0;j<y+1;j++) {
				System.out.print(sheet[i][j]);
				if(j!=y) {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		sc.close();
	}
} Main
import java.io.IOException;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) throws IOException{

		Scanner sc = new Scanner(System.in);

		int x = sc.nextInt();
		int y = sc.nextInt();

		int[][] sheet = new int[x+1][y+1];
		int[] row = new int[x];
		int[] col = new int[y];
		int total = 0;

		for(int i=0;i<x+1;i++) {
			for(int j=0;j<y+1;j++) {
				if(i!=x && j!=y){
					sheet[i][j] = sc.nextInt();
					total += sheet[i][j];
					row[i] += sheet[i][j];
					col[j] += sheet[i][j];
				}else if(i!=x && j==y) {
					sheet[i][j] = row[i];
				}else if(i==x && j!=y) {
					sheet[i][j] = col[j];
				}else if(i==x && j==y) {
					sheet[i][j] = total;
				}
			}
		}

		for(int i=0;i<x+1;i++) {
			for(int j=0;j<y+1;j++) {
				System.out.print(sheet[i][j]);
				if(j!=y) {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		sc.close();
	}
}
