import java.util.Scanner;


public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int i;
		int j;

		int r = sc.nextInt();
		int c = sc.nextInt();

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

		for(i = 0; i < r; i++) {
			for(j = 0; j < c; j++) {
				int a = sc.nextInt();
				result[i][j] = a;
				result[i][c] += a;
				result[r][j] += a;
				result[r][c] += a;
			}
		}

		for(i = 0; i < r + 1; i++) {
			for(j = 0; j < c + 1; j++) {

				if(j == c) {
					System.out.print(result[i][j]);
				} else {
					System.out.print(result[i][j] + " ");
				}

			}
			System.out.println();
		}

	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int i;
		int j;

		int r = sc.nextInt();
		int c = sc.nextInt();

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

		for(i = 0; i < r; i++) {
			for(j = 0; j < c; j++) {
				int a = sc.nextInt();
				result[i][j] = a;
				result[i][c] += a;
				result[r][j] += a;
				result[r][c] += a;
			}
		}

		for(i = 0; i < r + 1; i++) {
			for(j = 0; j < c + 1; j++) {

				if(j == c) {
					System.out.print(result[i][j]);
				} else {
					System.out.print(result[i][j] + " ");
				}

			}
			System.out.println();
		}

	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int i;
		int j;

		int r = sc.nextInt();
		int c = sc.nextInt();

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

		for(i = 0; i < r; i++) {
			for(j = 0; j < c; j++) {
				int a = sc.nextInt();
				result[i][j] = a;
				result[i][c] += a;
				result[r][j] += a;
				result[r][c] += a;
			}
		}

		for(i = 0; i < r + 1; i++) {
			for(j = 0; j < c + 1; j++) {

				if(j == c) {
					System.out.print(result[i][j]);
				} else {
					System.out.print(result[i][j] + " ");
				}

			}
			System.out.println();
		}

	} main {
		Scanner sc = new Scanner(System.in);

		int i;
		int j;

		int r = sc.nextInt();
		int c = sc.nextInt();

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

		for(i = 0; i < r; i++) {
			for(j = 0; j < c; j++) {
				int a = sc.nextInt();
				result[i][j] = a;
				result[i][c] += a;
				result[r][j] += a;
				result[r][c] += a;
			}
		}

		for(i = 0; i < r + 1; i++) {
			for(j = 0; j < c + 1; j++) {

				if(j == c) {
					System.out.print(result[i][j]);
				} else {
					System.out.print(result[i][j] + " ");
				}

			}
			System.out.println();
		}

	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int i; i int j; j int r = sc.nextInt(); r sc.nextInt() sc.nextInt sc int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int result[][] = new int[r + 1][c + 1]; result new int[r + 1][c + 1] r + 1 r 1 c + 1 c 1 for(i = 0; i < r; i++) {
			for(j = 0; j < c; j++) {
				int a = sc.nextInt();
				result[i][j] = a;
				result[i][c] += a;
				result[r][j] += a;
				result[r][c] += a;
			}
		} i = 0; i = 0 i 0 i < r i r i++ i++ i {
			for(j = 0; j < c; j++) {
				int a = sc.nextInt();
				result[i][j] = a;
				result[i][c] += a;
				result[r][j] += a;
				result[r][c] += a;
			}
		} {
			for(j = 0; j < c; j++) {
				int a = sc.nextInt();
				result[i][j] = a;
				result[i][c] += a;
				result[r][j] += a;
				result[r][c] += a;
			}
		} for(j = 0; j < c; j++) {
				int a = sc.nextInt();
				result[i][j] = a;
				result[i][c] += a;
				result[r][j] += a;
				result[r][c] += a;
			} j = 0; j = 0 j 0 j < c j c j++ j++ j {
				int a = sc.nextInt();
				result[i][j] = a;
				result[i][c] += a;
				result[r][j] += a;
				result[r][c] += a;
			} {
				int a = sc.nextInt();
				result[i][j] = a;
				result[i][c] += a;
				result[r][j] += a;
				result[r][c] += a;
			} int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc result[i][j] = a result[i][j] result[i] result i j a result[i][c] += a result[i][c] result[i] result i c a result[r][j] += a result[r][j] result[r] result r j a result[r][c] += a result[r][c] result[r] result r c a for(i = 0; i < r + 1; i++) {
			for(j = 0; j < c + 1; j++) {

				if(j == c) {
					System.out.print(result[i][j]);
				} else {
					System.out.print(result[i][j] + " ");
				}

			}
			System.out.println();
		} i = 0; i = 0 i 0 i < r + 1 i r + 1 r 1 i++ i++ i {
			for(j = 0; j < c + 1; j++) {

				if(j == c) {
					System.out.print(result[i][j]);
				} else {
					System.out.print(result[i][j] + " ");
				}

			}
			System.out.println();
		} {
			for(j = 0; j < c + 1; j++) {

				if(j == c) {
					System.out.print(result[i][j]);
				} else {
					System.out.print(result[i][j] + " ");
				}

			}
			System.out.println();
		} for(j = 0; j < c + 1; j++) {

				if(j == c) {
					System.out.print(result[i][j]);
				} else {
					System.out.print(result[i][j] + " ");
				}

			} j = 0; j = 0 j 0 j < c + 1 j c + 1 c 1 j++ j++ j {

				if(j == c) {
					System.out.print(result[i][j]);
				} else {
					System.out.print(result[i][j] + " ");
				}

			} {

				if(j == c) {
					System.out.print(result[i][j]);
				} else {
					System.out.print(result[i][j] + " ");
				}

			} if(j == c) {
					System.out.print(result[i][j]);
				} else {
					System.out.print(result[i][j] + " ");
				} j == c j c {
					System.out.print(result[i][j]);
				} System.out.print(result[i][j]) System.out.print System.out System System.out result[i][j] result[i] result i j {
					System.out.print(result[i][j] + " ");
				} System.out.print(result[i][j] + " ") System.out.print System.out System System.out result[i][j] + " " result[i][j] result[i] result i j " " System.out.println() System.out.println System.out System System.out String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int i;
		int j;

		int r = sc.nextInt();
		int c = sc.nextInt();

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

		for(i = 0; i < r; i++) {
			for(j = 0; j < c; j++) {
				int a = sc.nextInt();
				result[i][j] = a;
				result[i][c] += a;
				result[r][j] += a;
				result[r][c] += a;
			}
		}

		for(i = 0; i < r + 1; i++) {
			for(j = 0; j < c + 1; j++) {

				if(j == c) {
					System.out.print(result[i][j]);
				} else {
					System.out.print(result[i][j] + " ");
				}

			}
			System.out.println();
		}

	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int i;
		int j;

		int r = sc.nextInt();
		int c = sc.nextInt();

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

		for(i = 0; i < r; i++) {
			for(j = 0; j < c; j++) {
				int a = sc.nextInt();
				result[i][j] = a;
				result[i][c] += a;
				result[r][j] += a;
				result[r][c] += a;
			}
		}

		for(i = 0; i < r + 1; i++) {
			for(j = 0; j < c + 1; j++) {

				if(j == c) {
					System.out.print(result[i][j]);
				} else {
					System.out.print(result[i][j] + " ");
				}

			}
			System.out.println();
		}

	}
} Main
import java.util.Scanner;


public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int i;
		int j;

		int r = sc.nextInt();
		int c = sc.nextInt();

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

		for(i = 0; i < r; i++) {
			for(j = 0; j < c; j++) {
				int a = sc.nextInt();
				result[i][j] = a;
				result[i][c] += a;
				result[r][j] += a;
				result[r][c] += a;
			}
		}

		for(i = 0; i < r + 1; i++) {
			for(j = 0; j < c + 1; j++) {

				if(j == c) {
					System.out.print(result[i][j]);
				} else {
					System.out.print(result[i][j] + " ");
				}

			}
			System.out.println();
		}

	}
}