import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int a = Integer.parseInt(scan.next());
		int b = Integer.parseInt(scan.next());
		int[][] c= new int[a][b];
		int[] asum = new int[b];
		int[] bsum = new int[a];
		int sumall = 0;
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				c[i][j]=Integer.parseInt(scan.next());
				asum[j]+=c[i][j];
				bsum[i]+=c[i][j];
				sumall+=c[i][j];
			}
		}
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				System.out.print(c[i][j]+" ");
			}
			System.out.println(bsum[i]);
		}
		for(int i=0;i<b;i++) 
			System.out.print(asum[i]+" ");
		System.out.print(sumall+"\r\n");
	}	
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int a = Integer.parseInt(scan.next());
		int b = Integer.parseInt(scan.next());
		int[][] c= new int[a][b];
		int[] asum = new int[b];
		int[] bsum = new int[a];
		int sumall = 0;
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				c[i][j]=Integer.parseInt(scan.next());
				asum[j]+=c[i][j];
				bsum[i]+=c[i][j];
				sumall+=c[i][j];
			}
		}
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				System.out.print(c[i][j]+" ");
			}
			System.out.println(bsum[i]);
		}
		for(int i=0;i<b;i++) 
			System.out.print(asum[i]+" ");
		System.out.print(sumall+"\r\n");
	}	
} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int a = Integer.parseInt(scan.next());
		int b = Integer.parseInt(scan.next());
		int[][] c= new int[a][b];
		int[] asum = new int[b];
		int[] bsum = new int[a];
		int sumall = 0;
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				c[i][j]=Integer.parseInt(scan.next());
				asum[j]+=c[i][j];
				bsum[i]+=c[i][j];
				sumall+=c[i][j];
			}
		}
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				System.out.print(c[i][j]+" ");
			}
			System.out.println(bsum[i]);
		}
		for(int i=0;i<b;i++) 
			System.out.print(asum[i]+" ");
		System.out.print(sumall+"\r\n");
	} main {
		Scanner scan = new Scanner(System.in);
		int a = Integer.parseInt(scan.next());
		int b = Integer.parseInt(scan.next());
		int[][] c= new int[a][b];
		int[] asum = new int[b];
		int[] bsum = new int[a];
		int sumall = 0;
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				c[i][j]=Integer.parseInt(scan.next());
				asum[j]+=c[i][j];
				bsum[i]+=c[i][j];
				sumall+=c[i][j];
			}
		}
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				System.out.print(c[i][j]+" ");
			}
			System.out.println(bsum[i]);
		}
		for(int i=0;i<b;i++) 
			System.out.print(asum[i]+" ");
		System.out.print(sumall+"\r\n");
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int a = Integer.parseInt(scan.next()); a Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan int b = Integer.parseInt(scan.next()); b Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan int[][] c= new int[a][b]; c new int[a][b] a b int[] asum = new int[b]; asum new int[b] b int[] bsum = new int[a]; bsum new int[a] a int sumall = 0; sumall 0 for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				c[i][j]=Integer.parseInt(scan.next());
				asum[j]+=c[i][j];
				bsum[i]+=c[i][j];
				sumall+=c[i][j];
			}
		} int i=0; int i=0; i 0 i<a i a i++ i++ i {
			for(int j=0;j<b;j++){
				c[i][j]=Integer.parseInt(scan.next());
				asum[j]+=c[i][j];
				bsum[i]+=c[i][j];
				sumall+=c[i][j];
			}
		} {
			for(int j=0;j<b;j++){
				c[i][j]=Integer.parseInt(scan.next());
				asum[j]+=c[i][j];
				bsum[i]+=c[i][j];
				sumall+=c[i][j];
			}
		} for(int j=0;j<b;j++){
				c[i][j]=Integer.parseInt(scan.next());
				asum[j]+=c[i][j];
				bsum[i]+=c[i][j];
				sumall+=c[i][j];
			} int j=0; int j=0; j 0 j<b j b j++ j++ j {
				c[i][j]=Integer.parseInt(scan.next());
				asum[j]+=c[i][j];
				bsum[i]+=c[i][j];
				sumall+=c[i][j];
			} {
				c[i][j]=Integer.parseInt(scan.next());
				asum[j]+=c[i][j];
				bsum[i]+=c[i][j];
				sumall+=c[i][j];
			} c[i][j]=Integer.parseInt(scan.next()) c[i][j] c[i] c i j Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan asum[j]+=c[i][j] asum[j] asum j c[i][j] c[i] c i j bsum[i]+=c[i][j] bsum[i] bsum i c[i][j] c[i] c i j sumall+=c[i][j] sumall c[i][j] c[i] c i j for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				System.out.print(c[i][j]+" ");
			}
			System.out.println(bsum[i]);
		} int i=0; int i=0; i 0 i<a i a i++ i++ i {
			for(int j=0;j<b;j++){
				System.out.print(c[i][j]+" ");
			}
			System.out.println(bsum[i]);
		} {
			for(int j=0;j<b;j++){
				System.out.print(c[i][j]+" ");
			}
			System.out.println(bsum[i]);
		} for(int j=0;j<b;j++){
				System.out.print(c[i][j]+" ");
			} int j=0; int j=0; j 0 j<b j b j++ j++ j {
				System.out.print(c[i][j]+" ");
			} {
				System.out.print(c[i][j]+" ");
			} System.out.print(c[i][j]+" ") System.out.print System.out System System.out c[i][j]+" " c[i][j] c[i] c i j " " System.out.println(bsum[i]) System.out.println System.out System System.out bsum[i] bsum i for(int i=0;i<b;i++) 
			System.out.print(asum[i]+" "); int i=0; int i=0; i 0 i<b i b i++ i++ i System.out.print(asum[i]+" "); System.out.print(asum[i]+" ") System.out.print System.out System System.out asum[i]+" " asum[i] asum i " " System.out.print(sumall+"\r\n") System.out.print System.out System System.out sumall+"\r\n" sumall "\r\n" String[] args args public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int a = Integer.parseInt(scan.next());
		int b = Integer.parseInt(scan.next());
		int[][] c= new int[a][b];
		int[] asum = new int[b];
		int[] bsum = new int[a];
		int sumall = 0;
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				c[i][j]=Integer.parseInt(scan.next());
				asum[j]+=c[i][j];
				bsum[i]+=c[i][j];
				sumall+=c[i][j];
			}
		}
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				System.out.print(c[i][j]+" ");
			}
			System.out.println(bsum[i]);
		}
		for(int i=0;i<b;i++) 
			System.out.print(asum[i]+" ");
		System.out.print(sumall+"\r\n");
	}	
} public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int a = Integer.parseInt(scan.next());
		int b = Integer.parseInt(scan.next());
		int[][] c= new int[a][b];
		int[] asum = new int[b];
		int[] bsum = new int[a];
		int sumall = 0;
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				c[i][j]=Integer.parseInt(scan.next());
				asum[j]+=c[i][j];
				bsum[i]+=c[i][j];
				sumall+=c[i][j];
			}
		}
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				System.out.print(c[i][j]+" ");
			}
			System.out.println(bsum[i]);
		}
		for(int i=0;i<b;i++) 
			System.out.print(asum[i]+" ");
		System.out.print(sumall+"\r\n");
	}	
} Main
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int a = Integer.parseInt(scan.next());
		int b = Integer.parseInt(scan.next());
		int[][] c= new int[a][b];
		int[] asum = new int[b];
		int[] bsum = new int[a];
		int sumall = 0;
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				c[i][j]=Integer.parseInt(scan.next());
				asum[j]+=c[i][j];
				bsum[i]+=c[i][j];
				sumall+=c[i][j];
			}
		}
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				System.out.print(c[i][j]+" ");
			}
			System.out.println(bsum[i]);
		}
		for(int i=0;i<b;i++) 
			System.out.print(asum[i]+" ");
		System.out.print(sumall+"\r\n");
	}	
}