import java.util.Scanner;

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

public class Main {
	public static void main(String[] args){
		Scanner s = new Scanner(System.in);
		
		int r = s.nextInt();
		int c = s.nextInt();
		int[][] yousoA = new int[r][c];
		int[] yousoB = new int[r];
		int[] yousoB2 = new int[c];
		int sum = 0;
		
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				int x = s.nextInt();
				yousoA[i][j] = x;
				yousoB[i] += x;
				yousoB2[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(yousoA[i][j] + " ");
					else System.out.println(yousoB[i]);
				}
			else
				for(int k = 0; k < c; k++){
					System.out.print(yousoB2[k] + " ");
				}
		}
		System.out.println(sum);
		s.close();
	}
}
