import java.util.Scanner;

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

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		final int r = sc.nextInt();
		final int c = sc.nextInt();
		int[][] table = new int[r + 1][c + 1];
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				int v = sc.nextInt();
				table[i][j] = v;
				table[i][c] += v;
				table[r][j] += v;
				table[r][c] += v;
			}
		}
		sc.close();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i <= r; i++) {
			for (int j = 0; j <= c; j++) {
				sb.append(table[i][j]);
				if (j == c) {
					sb.append("\n");
				} else {
					sb.append(" ");
				}
			}
		}
		System.out.print(sb);
	}
}