import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws Exception {
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	    String[] line = br.readLine().split(" ");

	    int r = Integer.parseInt(line[0]);
	    int c = Integer.parseInt(line[1]);

	    int[][] matrix = new int[r + 1][c + 1];

	    for (int i = 0; i < r; i++) {
	    	line = br.readLine().split(" ");

	    	for (int j = 0; j < line.length; j++) {
		    	matrix[i][j] = Integer.parseInt(line[j]);

		    	// row sum
		    	matrix[i][c] += matrix[i][j];
		    	// col sum
		    	matrix[r][j] += matrix[i][j];

		    	if (j == line.length - 1) {
			    	// all sum
			    	matrix[r][c] += matrix[i][c];
		    	}
	    	}
	    }

	    for (int i = 0; i < r + 1; i++) {
	    	for (int j = 0; j < c + 1; j++) {
	    		System.out.print(matrix[i][j]);
	    		if (j < c) System.out.print(" ");
	    	}
	    	System.out.println("");
	    }
	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
	public static void main(String[] args) throws Exception {
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	    String[] line = br.readLine().split(" ");

	    int r = Integer.parseInt(line[0]);
	    int c = Integer.parseInt(line[1]);

	    int[][] matrix = new int[r + 1][c + 1];

	    for (int i = 0; i < r; i++) {
	    	line = br.readLine().split(" ");

	    	for (int j = 0; j < line.length; j++) {
		    	matrix[i][j] = Integer.parseInt(line[j]);

		    	// row sum
		    	matrix[i][c] += matrix[i][j];
		    	// col sum
		    	matrix[r][j] += matrix[i][j];

		    	if (j == line.length - 1) {
			    	// all sum
			    	matrix[r][c] += matrix[i][c];
		    	}
	    	}
	    }

	    for (int i = 0; i < r + 1; i++) {
	    	for (int j = 0; j < c + 1; j++) {
	    		System.out.print(matrix[i][j]);
	    		if (j < c) System.out.print(" ");
	    	}
	    	System.out.println("");
	    }
	}
} Main public static void main(String[] args) throws Exception {
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	    String[] line = br.readLine().split(" ");

	    int r = Integer.parseInt(line[0]);
	    int c = Integer.parseInt(line[1]);

	    int[][] matrix = new int[r + 1][c + 1];

	    for (int i = 0; i < r; i++) {
	    	line = br.readLine().split(" ");

	    	for (int j = 0; j < line.length; j++) {
		    	matrix[i][j] = Integer.parseInt(line[j]);

		    	// row sum
		    	matrix[i][c] += matrix[i][j];
		    	// col sum
		    	matrix[r][j] += matrix[i][j];

		    	if (j == line.length - 1) {
			    	// all sum
			    	matrix[r][c] += matrix[i][c];
		    	}
	    	}
	    }

	    for (int i = 0; i < r + 1; i++) {
	    	for (int j = 0; j < c + 1; j++) {
	    		System.out.print(matrix[i][j]);
	    		if (j < c) System.out.print(" ");
	    	}
	    	System.out.println("");
	    }
	} main {
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	    String[] line = br.readLine().split(" ");

	    int r = Integer.parseInt(line[0]);
	    int c = Integer.parseInt(line[1]);

	    int[][] matrix = new int[r + 1][c + 1];

	    for (int i = 0; i < r; i++) {
	    	line = br.readLine().split(" ");

	    	for (int j = 0; j < line.length; j++) {
		    	matrix[i][j] = Integer.parseInt(line[j]);

		    	// row sum
		    	matrix[i][c] += matrix[i][j];
		    	// col sum
		    	matrix[r][j] += matrix[i][j];

		    	if (j == line.length - 1) {
			    	// all sum
			    	matrix[r][c] += matrix[i][c];
		    	}
	    	}
	    }

	    for (int i = 0; i < r + 1; i++) {
	    	for (int j = 0; j < c + 1; j++) {
	    		System.out.print(matrix[i][j]);
	    		if (j < c) System.out.print(" ");
	    	}
	    	System.out.println("");
	    }
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] line = br.readLine().split(" "); line br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int r = Integer.parseInt(line[0]); r Integer.parseInt(line[0]) Integer.parseInt Integer line[0] line 0 int c = Integer.parseInt(line[1]); c Integer.parseInt(line[1]) Integer.parseInt Integer line[1] line 1 int[][] matrix = new int[r + 1][c + 1]; matrix new int[r + 1][c + 1] r + 1 r 1 c + 1 c 1 for (int i = 0; i < r; i++) {
	    	line = br.readLine().split(" ");

	    	for (int j = 0; j < line.length; j++) {
		    	matrix[i][j] = Integer.parseInt(line[j]);

		    	// row sum
		    	matrix[i][c] += matrix[i][j];
		    	// col sum
		    	matrix[r][j] += matrix[i][j];

		    	if (j == line.length - 1) {
			    	// all sum
			    	matrix[r][c] += matrix[i][c];
		    	}
	    	}
	    } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
	    	line = br.readLine().split(" ");

	    	for (int j = 0; j < line.length; j++) {
		    	matrix[i][j] = Integer.parseInt(line[j]);

		    	// row sum
		    	matrix[i][c] += matrix[i][j];
		    	// col sum
		    	matrix[r][j] += matrix[i][j];

		    	if (j == line.length - 1) {
			    	// all sum
			    	matrix[r][c] += matrix[i][c];
		    	}
	    	}
	    } {
	    	line = br.readLine().split(" ");

	    	for (int j = 0; j < line.length; j++) {
		    	matrix[i][j] = Integer.parseInt(line[j]);

		    	// row sum
		    	matrix[i][c] += matrix[i][j];
		    	// col sum
		    	matrix[r][j] += matrix[i][j];

		    	if (j == line.length - 1) {
			    	// all sum
			    	matrix[r][c] += matrix[i][c];
		    	}
	    	}
	    } line = br.readLine().split(" ") line br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " for (int j = 0; j < line.length; j++) {
		    	matrix[i][j] = Integer.parseInt(line[j]);

		    	// row sum
		    	matrix[i][c] += matrix[i][j];
		    	// col sum
		    	matrix[r][j] += matrix[i][j];

		    	if (j == line.length - 1) {
			    	// all sum
			    	matrix[r][c] += matrix[i][c];
		    	}
	    	} int j = 0; int j = 0; j 0 j < line.length j line.length line line.length j++ j++ j {
		    	matrix[i][j] = Integer.parseInt(line[j]);

		    	// row sum
		    	matrix[i][c] += matrix[i][j];
		    	// col sum
		    	matrix[r][j] += matrix[i][j];

		    	if (j == line.length - 1) {
			    	// all sum
			    	matrix[r][c] += matrix[i][c];
		    	}
	    	} {
		    	matrix[i][j] = Integer.parseInt(line[j]);

		    	// row sum
		    	matrix[i][c] += matrix[i][j];
		    	// col sum
		    	matrix[r][j] += matrix[i][j];

		    	if (j == line.length - 1) {
			    	// all sum
			    	matrix[r][c] += matrix[i][c];
		    	}
	    	} matrix[i][j] = Integer.parseInt(line[j]) matrix[i][j] matrix[i] matrix i j Integer.parseInt(line[j]) Integer.parseInt Integer line[j] line j matrix[i][c] += matrix[i][j] matrix[i][c] matrix[i] matrix i c matrix[i][j] matrix[i] matrix i j matrix[r][j] += matrix[i][j] matrix[r][j] matrix[r] matrix r j matrix[i][j] matrix[i] matrix i j if (j == line.length - 1) {
			    	// all sum
			    	matrix[r][c] += matrix[i][c];
		    	} j == line.length - 1 j line.length - 1 line.length line line.length 1 {
			    	// all sum
			    	matrix[r][c] += matrix[i][c];
		    	} matrix[r][c] += matrix[i][c] matrix[r][c] matrix[r] matrix r c matrix[i][c] matrix[i] matrix i c for (int i = 0; i < r + 1; i++) {
	    	for (int j = 0; j < c + 1; j++) {
	    		System.out.print(matrix[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(matrix[i][j]);
	    		if (j < c) System.out.print(" ");
	    	}
	    	System.out.println("");
	    } {
	    	for (int j = 0; j < c + 1; j++) {
	    		System.out.print(matrix[i][j]);
	    		if (j < c) System.out.print(" ");
	    	}
	    	System.out.println("");
	    } for (int j = 0; j < c + 1; j++) {
	    		System.out.print(matrix[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(matrix[i][j]);
	    		if (j < c) System.out.print(" ");
	    	} {
	    		System.out.print(matrix[i][j]);
	    		if (j < c) System.out.print(" ");
	    	} System.out.print(matrix[i][j]) System.out.print System.out System System.out matrix[i][j] matrix[i] matrix i j if (j < c) System.out.print(" "); j < c j c System.out.print(" ") System.out.print System.out System System.out " " System.out.println("") System.out.println System.out System System.out "" String[] args args public class Main {
	public static void main(String[] args) throws Exception {
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	    String[] line = br.readLine().split(" ");

	    int r = Integer.parseInt(line[0]);
	    int c = Integer.parseInt(line[1]);

	    int[][] matrix = new int[r + 1][c + 1];

	    for (int i = 0; i < r; i++) {
	    	line = br.readLine().split(" ");

	    	for (int j = 0; j < line.length; j++) {
		    	matrix[i][j] = Integer.parseInt(line[j]);

		    	// row sum
		    	matrix[i][c] += matrix[i][j];
		    	// col sum
		    	matrix[r][j] += matrix[i][j];

		    	if (j == line.length - 1) {
			    	// all sum
			    	matrix[r][c] += matrix[i][c];
		    	}
	    	}
	    }

	    for (int i = 0; i < r + 1; i++) {
	    	for (int j = 0; j < c + 1; j++) {
	    		System.out.print(matrix[i][j]);
	    		if (j < c) System.out.print(" ");
	    	}
	    	System.out.println("");
	    }
	}
} public class Main {
	public static void main(String[] args) throws Exception {
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	    String[] line = br.readLine().split(" ");

	    int r = Integer.parseInt(line[0]);
	    int c = Integer.parseInt(line[1]);

	    int[][] matrix = new int[r + 1][c + 1];

	    for (int i = 0; i < r; i++) {
	    	line = br.readLine().split(" ");

	    	for (int j = 0; j < line.length; j++) {
		    	matrix[i][j] = Integer.parseInt(line[j]);

		    	// row sum
		    	matrix[i][c] += matrix[i][j];
		    	// col sum
		    	matrix[r][j] += matrix[i][j];

		    	if (j == line.length - 1) {
			    	// all sum
			    	matrix[r][c] += matrix[i][c];
		    	}
	    	}
	    }

	    for (int i = 0; i < r + 1; i++) {
	    	for (int j = 0; j < c + 1; j++) {
	    		System.out.print(matrix[i][j]);
	    		if (j < c) System.out.print(" ");
	    	}
	    	System.out.println("");
	    }
	}
} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws Exception {
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	    String[] line = br.readLine().split(" ");

	    int r = Integer.parseInt(line[0]);
	    int c = Integer.parseInt(line[1]);

	    int[][] matrix = new int[r + 1][c + 1];

	    for (int i = 0; i < r; i++) {
	    	line = br.readLine().split(" ");

	    	for (int j = 0; j < line.length; j++) {
		    	matrix[i][j] = Integer.parseInt(line[j]);

		    	// row sum
		    	matrix[i][c] += matrix[i][j];
		    	// col sum
		    	matrix[r][j] += matrix[i][j];

		    	if (j == line.length - 1) {
			    	// all sum
			    	matrix[r][c] += matrix[i][c];
		    	}
	    	}
	    }

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