import java.util.Scanner;

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

public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int r = sc.nextInt();
		int c = sc.nextInt();
		int[][] spreadsheet = new int[r][c];
		int[][] sumSpreadsheet = new int[r + 1][c + 1];
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				spreadsheet[i][j] = sc.nextInt();
			}
		}
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sumSpreadsheet[i][j] = spreadsheet[i][j];
			}
		}
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sumSpreadsheet[i][c] += sumSpreadsheet[i][j];
			}
		}
		for (int j = 0; j < c; j++) {
			for (int i = 0; i < r; i++) {
				sumSpreadsheet[r][j] += sumSpreadsheet[i][j];
			}
		}
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sumSpreadsheet[r][c] += sumSpreadsheet[i][j];
			}
		}
		for (int i = 0; i < r + 1; i++) {
			for (int j = 0; j < c + 1; j++) {
				if (j == c) {
					System.out.print(sumSpreadsheet[i][j]);
				} else {
					System.out.print(sumSpreadsheet[i][j] + " ");
				}
			}
			System.out.println();
		}
		sc.close();
	}
}
