import java.util.Scanner;

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

		int[][] input = new int[r][c];
		int[] outputR = new int[r];
		int[] outputC = new int[c];

		for (int i = 0; i < r; i++)
			for (int i2 = 0; i2 < c; i2++)
				input[i][i2] = scanner.nextInt();

		for (int i = 0; i < r; i++) {
			for (int i2 = 0; i2 < c; i2++) {
				outputR[i] += input[i][i2];
				System.out.print(input[i][i2] + " ");
			}

			System.out.println(outputR[i]);
		}

		for (int i = 0; i < c; i++) {
			for (int i2 = 0; i2 < r; i2++)
				outputC[i] += input[i2][i];

			total += outputC[i];
			System.out.print(outputC[i] + " ");
		}

		System.out.println(total);
	}
} import java.util.Scanner; Scanner java.util class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int r = scanner.nextInt(), c = scanner.nextInt(), total = 0;

		int[][] input = new int[r][c];
		int[] outputR = new int[r];
		int[] outputC = new int[c];

		for (int i = 0; i < r; i++)
			for (int i2 = 0; i2 < c; i2++)
				input[i][i2] = scanner.nextInt();

		for (int i = 0; i < r; i++) {
			for (int i2 = 0; i2 < c; i2++) {
				outputR[i] += input[i][i2];
				System.out.print(input[i][i2] + " ");
			}

			System.out.println(outputR[i]);
		}

		for (int i = 0; i < c; i++) {
			for (int i2 = 0; i2 < r; i2++)
				outputC[i] += input[i2][i];

			total += outputC[i];
			System.out.print(outputC[i] + " ");
		}

		System.out.println(total);
	}
} Main public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int r = scanner.nextInt(), c = scanner.nextInt(), total = 0;

		int[][] input = new int[r][c];
		int[] outputR = new int[r];
		int[] outputC = new int[c];

		for (int i = 0; i < r; i++)
			for (int i2 = 0; i2 < c; i2++)
				input[i][i2] = scanner.nextInt();

		for (int i = 0; i < r; i++) {
			for (int i2 = 0; i2 < c; i2++) {
				outputR[i] += input[i][i2];
				System.out.print(input[i][i2] + " ");
			}

			System.out.println(outputR[i]);
		}

		for (int i = 0; i < c; i++) {
			for (int i2 = 0; i2 < r; i2++)
				outputC[i] += input[i2][i];

			total += outputC[i];
			System.out.print(outputC[i] + " ");
		}

		System.out.println(total);
	} main {
		Scanner scanner = new Scanner(System.in);
		int r = scanner.nextInt(), c = scanner.nextInt(), total = 0;

		int[][] input = new int[r][c];
		int[] outputR = new int[r];
		int[] outputC = new int[c];

		for (int i = 0; i < r; i++)
			for (int i2 = 0; i2 < c; i2++)
				input[i][i2] = scanner.nextInt();

		for (int i = 0; i < r; i++) {
			for (int i2 = 0; i2 < c; i2++) {
				outputR[i] += input[i][i2];
				System.out.print(input[i][i2] + " ");
			}

			System.out.println(outputR[i]);
		}

		for (int i = 0; i < c; i++) {
			for (int i2 = 0; i2 < r; i2++)
				outputC[i] += input[i2][i];

			total += outputC[i];
			System.out.print(outputC[i] + " ");
		}

		System.out.println(total);
	} Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int r = scanner.nextInt() r scanner.nextInt() scanner.nextInt scanner c = scanner.nextInt() c scanner.nextInt() scanner.nextInt scanner total = 0; total 0 int[][] input = new int[r][c]; input new int[r][c] r c int[] outputR = new int[r]; outputR new int[r] r int[] outputC = new int[c]; outputC new int[c] c for (int i = 0; i < r; i++)
			for (int i2 = 0; i2 < c; i2++)
				input[i][i2] = scanner.nextInt(); int i = 0; int i = 0; i 0 i < r i r i++ i++ i for (int i2 = 0; i2 < c; i2++)
				input[i][i2] = scanner.nextInt(); for (int i2 = 0; i2 < c; i2++)
				input[i][i2] = scanner.nextInt(); int i2 = 0; int i2 = 0; i2 0 i2 < c i2 c i2++ i2++ i2 input[i][i2] = scanner.nextInt(); input[i][i2] = scanner.nextInt() input[i][i2] input[i] input i i2 scanner.nextInt() scanner.nextInt scanner for (int i = 0; i < r; i++) {
			for (int i2 = 0; i2 < c; i2++) {
				outputR[i] += input[i][i2];
				System.out.print(input[i][i2] + " ");
			}

			System.out.println(outputR[i]);
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			for (int i2 = 0; i2 < c; i2++) {
				outputR[i] += input[i][i2];
				System.out.print(input[i][i2] + " ");
			}

			System.out.println(outputR[i]);
		} {
			for (int i2 = 0; i2 < c; i2++) {
				outputR[i] += input[i][i2];
				System.out.print(input[i][i2] + " ");
			}

			System.out.println(outputR[i]);
		} for (int i2 = 0; i2 < c; i2++) {
				outputR[i] += input[i][i2];
				System.out.print(input[i][i2] + " ");
			} int i2 = 0; int i2 = 0; i2 0 i2 < c i2 c i2++ i2++ i2 {
				outputR[i] += input[i][i2];
				System.out.print(input[i][i2] + " ");
			} {
				outputR[i] += input[i][i2];
				System.out.print(input[i][i2] + " ");
			} outputR[i] += input[i][i2] outputR[i] outputR i input[i][i2] input[i] input i i2 System.out.print(input[i][i2] + " ") System.out.print System.out System System.out input[i][i2] + " " input[i][i2] input[i] input i i2 " " System.out.println(outputR[i]) System.out.println System.out System System.out outputR[i] outputR i for (int i = 0; i < c; i++) {
			for (int i2 = 0; i2 < r; i2++)
				outputC[i] += input[i2][i];

			total += outputC[i];
			System.out.print(outputC[i] + " ");
		} int i = 0; int i = 0; i 0 i < c i c i++ i++ i {
			for (int i2 = 0; i2 < r; i2++)
				outputC[i] += input[i2][i];

			total += outputC[i];
			System.out.print(outputC[i] + " ");
		} {
			for (int i2 = 0; i2 < r; i2++)
				outputC[i] += input[i2][i];

			total += outputC[i];
			System.out.print(outputC[i] + " ");
		} for (int i2 = 0; i2 < r; i2++)
				outputC[i] += input[i2][i]; int i2 = 0; int i2 = 0; i2 0 i2 < r i2 r i2++ i2++ i2 outputC[i] += input[i2][i]; outputC[i] += input[i2][i] outputC[i] outputC i input[i2][i] input[i2] input i2 i total += outputC[i] total outputC[i] outputC i System.out.print(outputC[i] + " ") System.out.print System.out System System.out outputC[i] + " " outputC[i] outputC i " " System.out.println(total) System.out.println System.out System System.out total String[] args args
import java.util.Scanner;

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

		int[][] input = new int[r][c];
		int[] outputR = new int[r];
		int[] outputC = new int[c];

		for (int i = 0; i < r; i++)
			for (int i2 = 0; i2 < c; i2++)
				input[i][i2] = scanner.nextInt();

		for (int i = 0; i < r; i++) {
			for (int i2 = 0; i2 < c; i2++) {
				outputR[i] += input[i][i2];
				System.out.print(input[i][i2] + " ");
			}

			System.out.println(outputR[i]);
		}

		for (int i = 0; i < c; i++) {
			for (int i2 = 0; i2 < r; i2++)
				outputC[i] += input[i2][i];

			total += outputC[i];
			System.out.print(outputC[i] + " ");
		}

		System.out.println(total);
	}
}