import java.util.*;

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

class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		int r = stdIn.nextInt();
		int c = stdIn.nextInt();
		int[][] A = new int[r][c];
		int[] b = new int[r];
		int[] b2 = new int[c];
		int sum = 0;
		for (int i=0; i<r; i++) {
			for (int j=0; j<c; j++) {
				int x = stdIn.nextInt();
				A[i][j] = x;
				b[i] += x;
				b2[j] += x;
				sum += x;
			}
		}
		for (int i=0; i<r+1; i++) {
			if(i<r)
				for (int j=0; j<c+1; j++) {
					if(j<c)
						System.out.print(A[i][j]+" ");
					else
						System.out.println(b[i]);
				}
			else
				for (int k=0; k<c; k++) {
					System.out.print(b2[k]+" ");
				}
		}
		System.out.println(sum);
	}
}