import java.util.Scanner;

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

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

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

		for(int i = 0; i < r; i++) {
			int sum = 0;
			for(int j = 0; j < c; j++) {
				table[i][j] = scan.nextInt();
				sum += table[i][j];
				System.out.printf(table[i][j] + " ");
			}
			table[i][c] = sum;
			System.out.println(sum);
		}

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

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

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

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

		for(int i = 0; i < r; i++) {
			int sum = 0;
			for(int j = 0; j < c; j++) {
				table[i][j] = scan.nextInt();
				sum += table[i][j];
				System.out.printf(table[i][j] + " ");
			}
			table[i][c] = sum;
			System.out.println(sum);
		}

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

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

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

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

		for(int i = 0; i < r; i++) {
			int sum = 0;
			for(int j = 0; j < c; j++) {
				table[i][j] = scan.nextInt();
				sum += table[i][j];
				System.out.printf(table[i][j] + " ");
			}
			table[i][c] = sum;
			System.out.println(sum);
		}

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

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

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

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

		for(int i = 0; i < r; i++) {
			int sum = 0;
			for(int j = 0; j < c; j++) {
				table[i][j] = scan.nextInt();
				sum += table[i][j];
				System.out.printf(table[i][j] + " ");
			}
			table[i][c] = sum;
			System.out.println(sum);
		}

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

		scan.close();
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int r = scan.nextInt(); r scan.nextInt() scan.nextInt scan int c = scan.nextInt(); c scan.nextInt() scan.nextInt scan int[][] table = new int[r][c + 1]; table new int[r][c + 1] r c + 1 c 1 for(int i = 0; i < r; i++) {
			int sum = 0;
			for(int j = 0; j < c; j++) {
				table[i][j] = scan.nextInt();
				sum += table[i][j];
				System.out.printf(table[i][j] + " ");
			}
			table[i][c] = sum;
			System.out.println(sum);
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			int sum = 0;
			for(int j = 0; j < c; j++) {
				table[i][j] = scan.nextInt();
				sum += table[i][j];
				System.out.printf(table[i][j] + " ");
			}
			table[i][c] = sum;
			System.out.println(sum);
		} {
			int sum = 0;
			for(int j = 0; j < c; j++) {
				table[i][j] = scan.nextInt();
				sum += table[i][j];
				System.out.printf(table[i][j] + " ");
			}
			table[i][c] = sum;
			System.out.println(sum);
		} int sum = 0; sum 0 for(int j = 0; j < c; j++) {
				table[i][j] = scan.nextInt();
				sum += table[i][j];
				System.out.printf(table[i][j] + " ");
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				table[i][j] = scan.nextInt();
				sum += table[i][j];
				System.out.printf(table[i][j] + " ");
			} {
				table[i][j] = scan.nextInt();
				sum += table[i][j];
				System.out.printf(table[i][j] + " ");
			} table[i][j] = scan.nextInt() table[i][j] table[i] table i j scan.nextInt() scan.nextInt scan sum += table[i][j] sum table[i][j] table[i] table i j System.out.printf(table[i][j] + " ") System.out.printf System.out System System.out table[i][j] + " " table[i][j] table[i] table i j " " table[i][c] = sum table[i][c] table[i] table i c sum System.out.println(sum) System.out.println System.out System System.out sum for(int i = 0; i < c + 1; i++) {
			int sum = 0;
			for(int j = 0; j < r; j++) {
				sum += table[j][i];
			}
			if(i != 0) {
				System.out.printf(" ");
			}
			System.out.printf(sum + "");
		} int i = 0; int i = 0; i 0 i < c + 1 i c + 1 c 1 i++ i++ i {
			int sum = 0;
			for(int j = 0; j < r; j++) {
				sum += table[j][i];
			}
			if(i != 0) {
				System.out.printf(" ");
			}
			System.out.printf(sum + "");
		} {
			int sum = 0;
			for(int j = 0; j < r; j++) {
				sum += table[j][i];
			}
			if(i != 0) {
				System.out.printf(" ");
			}
			System.out.printf(sum + "");
		} int sum = 0; sum 0 for(int j = 0; j < r; j++) {
				sum += table[j][i];
			} int j = 0; int j = 0; j 0 j < r j r j++ j++ j {
				sum += table[j][i];
			} {
				sum += table[j][i];
			} sum += table[j][i] sum table[j][i] table[j] table j i if(i != 0) {
				System.out.printf(" ");
			} i != 0 i 0 {
				System.out.printf(" ");
			} System.out.printf(" ") System.out.printf System.out System System.out " " System.out.printf(sum + "") System.out.printf System.out System System.out sum + "" sum "" System.out.println() System.out.println System.out System System.out scan.close() scan.close scan String[] args args public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

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

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

		for(int i = 0; i < r; i++) {
			int sum = 0;
			for(int j = 0; j < c; j++) {
				table[i][j] = scan.nextInt();
				sum += table[i][j];
				System.out.printf(table[i][j] + " ");
			}
			table[i][c] = sum;
			System.out.println(sum);
		}

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

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

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

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

		for(int i = 0; i < r; i++) {
			int sum = 0;
			for(int j = 0; j < c; j++) {
				table[i][j] = scan.nextInt();
				sum += table[i][j];
				System.out.printf(table[i][j] + " ");
			}
			table[i][c] = sum;
			System.out.println(sum);
		}

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

		scan.close();
	}
} Main
import java.util.Scanner;

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

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

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

		for(int i = 0; i < r; i++) {
			int sum = 0;
			for(int j = 0; j < c; j++) {
				table[i][j] = scan.nextInt();
				sum += table[i][j];
				System.out.printf(table[i][j] + " ");
			}
			table[i][c] = sum;
			System.out.println(sum);
		}

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

		scan.close();
	}
}
