import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int r = scanner.nextInt();
		int c = scanner.nextInt();
		int[][] arr = new int[r+1][c+1];
		for(int i=0;i<r;i++) {
			int sumX = 0;
			for(int j=0;j<c;j++) {
				arr[i][j] = scanner.nextInt();
				sumX += arr[i][j];
			}
			arr[i][c] = sumX;
		}
		for(int i=0;i<c+1;i++) {
			int sumY = 0;
			for(int j=0;j<r+1;j++) {
				sumY += arr[j][i];
			}
			arr[r][i] = sumY;
		}
		
		for(int i=0;i<r+1;i++) {
			for(int j=0;j<c+1;j++) {
				if (j!=c) {
					System.out.print(arr[i][j] + " ");
				}else {
					System.out.print(arr[i][j]);
				}
			}
			System.out.println();
		}
		scanner.close();
	}
} import java.util.Scanner; Scanner java.util public class Main {

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

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

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int r = scanner.nextInt();
		int c = scanner.nextInt();
		int[][] arr = new int[r+1][c+1];
		for(int i=0;i<r;i++) {
			int sumX = 0;
			for(int j=0;j<c;j++) {
				arr[i][j] = scanner.nextInt();
				sumX += arr[i][j];
			}
			arr[i][c] = sumX;
		}
		for(int i=0;i<c+1;i++) {
			int sumY = 0;
			for(int j=0;j<r+1;j++) {
				sumY += arr[j][i];
			}
			arr[r][i] = sumY;
		}
		
		for(int i=0;i<r+1;i++) {
			for(int j=0;j<c+1;j++) {
				if (j!=c) {
					System.out.print(arr[i][j] + " ");
				}else {
					System.out.print(arr[i][j]);
				}
			}
			System.out.println();
		}
		scanner.close();
	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int r = scanner.nextInt();
		int c = scanner.nextInt();
		int[][] arr = new int[r+1][c+1];
		for(int i=0;i<r;i++) {
			int sumX = 0;
			for(int j=0;j<c;j++) {
				arr[i][j] = scanner.nextInt();
				sumX += arr[i][j];
			}
			arr[i][c] = sumX;
		}
		for(int i=0;i<c+1;i++) {
			int sumY = 0;
			for(int j=0;j<r+1;j++) {
				sumY += arr[j][i];
			}
			arr[r][i] = sumY;
		}
		
		for(int i=0;i<r+1;i++) {
			for(int j=0;j<c+1;j++) {
				if (j!=c) {
					System.out.print(arr[i][j] + " ");
				}else {
					System.out.print(arr[i][j]);
				}
			}
			System.out.println();
		}
		scanner.close();
	}
}
