import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		StringBuilder output = new StringBuilder();
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] sheet = new int[r+1][c+1];
		
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sheet[i][j] = scan.nextInt();
			}
		}
		
		// 表の端に合計を記録
		int sum_r = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum_r += sheet[i][j];
			}
			sheet[i][c] = sum_r;
			sum_r = 0;
		}
		int sum_c = 0;
		for (int j = 0; j < c; j++) {
			for (int i = 0; i < r; i++) {
				sum_c += sheet[i][j];
			}
			sheet[r][j] = sum_c;
			sum_c = 0;
		}
		int sum_all = 0;
		for (int i = 0; i < r; i++) {
			sum_all += sheet[i][c];
		}
		sheet[r][c] = sum_all;
		
		// 出力
		for (int i = 0; i < r+1; i++) {
			output.append(sheet[i][0]);
			for (int j = 1; j < c+1; j++) {
				output.append(" ").append(sheet[i][j]);
			}
			output.append('\n');
		}
		System.out.print(output);
		scan.close();
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		StringBuilder output = new StringBuilder();
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] sheet = new int[r+1][c+1];
		
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sheet[i][j] = scan.nextInt();
			}
		}
		
		// 表の端に合計を記録
		int sum_r = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum_r += sheet[i][j];
			}
			sheet[i][c] = sum_r;
			sum_r = 0;
		}
		int sum_c = 0;
		for (int j = 0; j < c; j++) {
			for (int i = 0; i < r; i++) {
				sum_c += sheet[i][j];
			}
			sheet[r][j] = sum_c;
			sum_c = 0;
		}
		int sum_all = 0;
		for (int i = 0; i < r; i++) {
			sum_all += sheet[i][c];
		}
		sheet[r][c] = sum_all;
		
		// 出力
		for (int i = 0; i < r+1; i++) {
			output.append(sheet[i][0]);
			for (int j = 1; j < c+1; j++) {
				output.append(" ").append(sheet[i][j]);
			}
			output.append('\n');
		}
		System.out.print(output);
		scan.close();
	}
} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		StringBuilder output = new StringBuilder();
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] sheet = new int[r+1][c+1];
		
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sheet[i][j] = scan.nextInt();
			}
		}
		
		// 表の端に合計を記録
		int sum_r = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum_r += sheet[i][j];
			}
			sheet[i][c] = sum_r;
			sum_r = 0;
		}
		int sum_c = 0;
		for (int j = 0; j < c; j++) {
			for (int i = 0; i < r; i++) {
				sum_c += sheet[i][j];
			}
			sheet[r][j] = sum_c;
			sum_c = 0;
		}
		int sum_all = 0;
		for (int i = 0; i < r; i++) {
			sum_all += sheet[i][c];
		}
		sheet[r][c] = sum_all;
		
		// 出力
		for (int i = 0; i < r+1; i++) {
			output.append(sheet[i][0]);
			for (int j = 1; j < c+1; j++) {
				output.append(" ").append(sheet[i][j]);
			}
			output.append('\n');
		}
		System.out.print(output);
		scan.close();
	} main {
		Scanner scan = new Scanner(System.in);
		StringBuilder output = new StringBuilder();
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] sheet = new int[r+1][c+1];
		
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sheet[i][j] = scan.nextInt();
			}
		}
		
		// 表の端に合計を記録
		int sum_r = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum_r += sheet[i][j];
			}
			sheet[i][c] = sum_r;
			sum_r = 0;
		}
		int sum_c = 0;
		for (int j = 0; j < c; j++) {
			for (int i = 0; i < r; i++) {
				sum_c += sheet[i][j];
			}
			sheet[r][j] = sum_c;
			sum_c = 0;
		}
		int sum_all = 0;
		for (int i = 0; i < r; i++) {
			sum_all += sheet[i][c];
		}
		sheet[r][c] = sum_all;
		
		// 出力
		for (int i = 0; i < r+1; i++) {
			output.append(sheet[i][0]);
			for (int j = 1; j < c+1; j++) {
				output.append(" ").append(sheet[i][j]);
			}
			output.append('\n');
		}
		System.out.print(output);
		scan.close();
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) StringBuilder output = new StringBuilder(); output new StringBuilder() int r = scan.nextInt(); r scan.nextInt() scan.nextInt scan int c = scan.nextInt(); c scan.nextInt() scan.nextInt scan int[][] sheet = new int[r+1][c+1]; sheet 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++) {
				sheet[i][j] = scan.nextInt();
			}
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			for (int j = 0; j < c; j++) {
				sheet[i][j] = scan.nextInt();
			}
		} {
			for (int j = 0; j < c; j++) {
				sheet[i][j] = scan.nextInt();
			}
		} for (int j = 0; j < c; j++) {
				sheet[i][j] = scan.nextInt();
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				sheet[i][j] = scan.nextInt();
			} {
				sheet[i][j] = scan.nextInt();
			} sheet[i][j] = scan.nextInt() sheet[i][j] sheet[i] sheet i j scan.nextInt() scan.nextInt scan int sum_r = 0; sum_r 0 for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum_r += sheet[i][j];
			}
			sheet[i][c] = sum_r;
			sum_r = 0;
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			for (int j = 0; j < c; j++) {
				sum_r += sheet[i][j];
			}
			sheet[i][c] = sum_r;
			sum_r = 0;
		} {
			for (int j = 0; j < c; j++) {
				sum_r += sheet[i][j];
			}
			sheet[i][c] = sum_r;
			sum_r = 0;
		} for (int j = 0; j < c; j++) {
				sum_r += sheet[i][j];
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				sum_r += sheet[i][j];
			} {
				sum_r += sheet[i][j];
			} sum_r += sheet[i][j] sum_r sheet[i][j] sheet[i] sheet i j sheet[i][c] = sum_r sheet[i][c] sheet[i] sheet i c sum_r sum_r = 0 sum_r 0 int sum_c = 0; sum_c 0 for (int j = 0; j < c; j++) {
			for (int i = 0; i < r; i++) {
				sum_c += sheet[i][j];
			}
			sheet[r][j] = sum_c;
			sum_c = 0;
		} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
			for (int i = 0; i < r; i++) {
				sum_c += sheet[i][j];
			}
			sheet[r][j] = sum_c;
			sum_c = 0;
		} {
			for (int i = 0; i < r; i++) {
				sum_c += sheet[i][j];
			}
			sheet[r][j] = sum_c;
			sum_c = 0;
		} for (int i = 0; i < r; i++) {
				sum_c += sheet[i][j];
			} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
				sum_c += sheet[i][j];
			} {
				sum_c += sheet[i][j];
			} sum_c += sheet[i][j] sum_c sheet[i][j] sheet[i] sheet i j sheet[r][j] = sum_c sheet[r][j] sheet[r] sheet r j sum_c sum_c = 0 sum_c 0 int sum_all = 0; sum_all 0 for (int i = 0; i < r; i++) {
			sum_all += sheet[i][c];
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			sum_all += sheet[i][c];
		} {
			sum_all += sheet[i][c];
		} sum_all += sheet[i][c] sum_all sheet[i][c] sheet[i] sheet i c sheet[r][c] = sum_all sheet[r][c] sheet[r] sheet r c sum_all for (int i = 0; i < r+1; i++) {
			output.append(sheet[i][0]);
			for (int j = 1; j < c+1; j++) {
				output.append(" ").append(sheet[i][j]);
			}
			output.append('\n');
		} int i = 0; int i = 0; i 0 i < r+1 i r+1 r 1 i++ i++ i {
			output.append(sheet[i][0]);
			for (int j = 1; j < c+1; j++) {
				output.append(" ").append(sheet[i][j]);
			}
			output.append('\n');
		} {
			output.append(sheet[i][0]);
			for (int j = 1; j < c+1; j++) {
				output.append(" ").append(sheet[i][j]);
			}
			output.append('\n');
		} output.append(sheet[i][0]) output.append output sheet[i][0] sheet[i] sheet i 0 for (int j = 1; j < c+1; j++) {
				output.append(" ").append(sheet[i][j]);
			} int j = 1; int j = 1; j 1 j < c+1 j c+1 c 1 j++ j++ j {
				output.append(" ").append(sheet[i][j]);
			} {
				output.append(" ").append(sheet[i][j]);
			} output.append(" ").append(sheet[i][j]) output.append(" ").append output.append(" ") output.append output " " sheet[i][j] sheet[i] sheet i j output.append('\n') output.append output '\n' System.out.print(output) System.out.print System.out System System.out output scan.close() scan.close scan String[] args args public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		StringBuilder output = new StringBuilder();
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] sheet = new int[r+1][c+1];
		
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sheet[i][j] = scan.nextInt();
			}
		}
		
		// 表の端に合計を記録
		int sum_r = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum_r += sheet[i][j];
			}
			sheet[i][c] = sum_r;
			sum_r = 0;
		}
		int sum_c = 0;
		for (int j = 0; j < c; j++) {
			for (int i = 0; i < r; i++) {
				sum_c += sheet[i][j];
			}
			sheet[r][j] = sum_c;
			sum_c = 0;
		}
		int sum_all = 0;
		for (int i = 0; i < r; i++) {
			sum_all += sheet[i][c];
		}
		sheet[r][c] = sum_all;
		
		// 出力
		for (int i = 0; i < r+1; i++) {
			output.append(sheet[i][0]);
			for (int j = 1; j < c+1; j++) {
				output.append(" ").append(sheet[i][j]);
			}
			output.append('\n');
		}
		System.out.print(output);
		scan.close();
	}
} public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		StringBuilder output = new StringBuilder();
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] sheet = new int[r+1][c+1];
		
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sheet[i][j] = scan.nextInt();
			}
		}
		
		// 表の端に合計を記録
		int sum_r = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum_r += sheet[i][j];
			}
			sheet[i][c] = sum_r;
			sum_r = 0;
		}
		int sum_c = 0;
		for (int j = 0; j < c; j++) {
			for (int i = 0; i < r; i++) {
				sum_c += sheet[i][j];
			}
			sheet[r][j] = sum_c;
			sum_c = 0;
		}
		int sum_all = 0;
		for (int i = 0; i < r; i++) {
			sum_all += sheet[i][c];
		}
		sheet[r][c] = sum_all;
		
		// 出力
		for (int i = 0; i < r+1; i++) {
			output.append(sheet[i][0]);
			for (int j = 1; j < c+1; j++) {
				output.append(" ").append(sheet[i][j]);
			}
			output.append('\n');
		}
		System.out.print(output);
		scan.close();
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		StringBuilder output = new StringBuilder();
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] sheet = new int[r+1][c+1];
		
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sheet[i][j] = scan.nextInt();
			}
		}
		
		// 表の端に合計を記録
		int sum_r = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum_r += sheet[i][j];
			}
			sheet[i][c] = sum_r;
			sum_r = 0;
		}
		int sum_c = 0;
		for (int j = 0; j < c; j++) {
			for (int i = 0; i < r; i++) {
				sum_c += sheet[i][j];
			}
			sheet[r][j] = sum_c;
			sum_c = 0;
		}
		int sum_all = 0;
		for (int i = 0; i < r; i++) {
			sum_all += sheet[i][c];
		}
		sheet[r][c] = sum_all;
		
		// 出力
		for (int i = 0; i < r+1; i++) {
			output.append(sheet[i][0]);
			for (int j = 1; j < c+1; j++) {
				output.append(" ").append(sheet[i][j]);
			}
			output.append('\n');
		}
		System.out.print(output);
		scan.close();
	}
}