import java.util.Scanner;
public class Main {
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int k[] = new int[(r+1)*(c+1)];
		for(int a = 0;a<r*c;a++){
			k[a] = scan.nextInt();
		}
		int kaunto = 0;
		int tate = 0;
		int tateR = 0;
		for(int b = 0;b<r;b++){
			int yoko = 0;
			for(int d = 0;d<(c+1);d++){
				if(d != c){
					System.out.print(k[kaunto] + " ");
					yoko = yoko + k[kaunto];
					++kaunto;
				}else{
					System.out.println(yoko);
					tateR = tateR + yoko;
				}
			}
		}
		for(int e = 0;e<(c+1);e++){
			if(e != c){
				for(int f = 0;f<r*c;f++){
					if(f % c == e){
						tate = tate + k[f];
					}
				}
				System.out.print(tate + " ");
				tate = 0;
			}else{
				System.out.println(tateR);
			}
		}
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int k[] = new int[(r+1)*(c+1)];
		for(int a = 0;a<r*c;a++){
			k[a] = scan.nextInt();
		}
		int kaunto = 0;
		int tate = 0;
		int tateR = 0;
		for(int b = 0;b<r;b++){
			int yoko = 0;
			for(int d = 0;d<(c+1);d++){
				if(d != c){
					System.out.print(k[kaunto] + " ");
					yoko = yoko + k[kaunto];
					++kaunto;
				}else{
					System.out.println(yoko);
					tateR = tateR + yoko;
				}
			}
		}
		for(int e = 0;e<(c+1);e++){
			if(e != c){
				for(int f = 0;f<r*c;f++){
					if(f % c == e){
						tate = tate + k[f];
					}
				}
				System.out.print(tate + " ");
				tate = 0;
			}else{
				System.out.println(tateR);
			}
		}
	}
} Main public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int k[] = new int[(r+1)*(c+1)];
		for(int a = 0;a<r*c;a++){
			k[a] = scan.nextInt();
		}
		int kaunto = 0;
		int tate = 0;
		int tateR = 0;
		for(int b = 0;b<r;b++){
			int yoko = 0;
			for(int d = 0;d<(c+1);d++){
				if(d != c){
					System.out.print(k[kaunto] + " ");
					yoko = yoko + k[kaunto];
					++kaunto;
				}else{
					System.out.println(yoko);
					tateR = tateR + yoko;
				}
			}
		}
		for(int e = 0;e<(c+1);e++){
			if(e != c){
				for(int f = 0;f<r*c;f++){
					if(f % c == e){
						tate = tate + k[f];
					}
				}
				System.out.print(tate + " ");
				tate = 0;
			}else{
				System.out.println(tateR);
			}
		}
	} main {
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int k[] = new int[(r+1)*(c+1)];
		for(int a = 0;a<r*c;a++){
			k[a] = scan.nextInt();
		}
		int kaunto = 0;
		int tate = 0;
		int tateR = 0;
		for(int b = 0;b<r;b++){
			int yoko = 0;
			for(int d = 0;d<(c+1);d++){
				if(d != c){
					System.out.print(k[kaunto] + " ");
					yoko = yoko + k[kaunto];
					++kaunto;
				}else{
					System.out.println(yoko);
					tateR = tateR + yoko;
				}
			}
		}
		for(int e = 0;e<(c+1);e++){
			if(e != c){
				for(int f = 0;f<r*c;f++){
					if(f % c == e){
						tate = tate + k[f];
					}
				}
				System.out.print(tate + " ");
				tate = 0;
			}else{
				System.out.println(tateR);
			}
		}
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int r = scan.nextInt(); r scan.nextInt() scan.nextInt scan int c = scan.nextInt(); c scan.nextInt() scan.nextInt scan int k[] = new int[(r+1)*(c+1)]; k new int[(r+1)*(c+1)] (r+1)*(c+1) (r+1) r 1 (c+1) c 1 for(int a = 0;a<r*c;a++){
			k[a] = scan.nextInt();
		} int a = 0; int a = 0; a 0 a<r*c a r*c r c a++ a++ a {
			k[a] = scan.nextInt();
		} {
			k[a] = scan.nextInt();
		} k[a] = scan.nextInt() k[a] k a scan.nextInt() scan.nextInt scan int kaunto = 0; kaunto 0 int tate = 0; tate 0 int tateR = 0; tateR 0 for(int b = 0;b<r;b++){
			int yoko = 0;
			for(int d = 0;d<(c+1);d++){
				if(d != c){
					System.out.print(k[kaunto] + " ");
					yoko = yoko + k[kaunto];
					++kaunto;
				}else{
					System.out.println(yoko);
					tateR = tateR + yoko;
				}
			}
		} int b = 0; int b = 0; b 0 b<r b r b++ b++ b {
			int yoko = 0;
			for(int d = 0;d<(c+1);d++){
				if(d != c){
					System.out.print(k[kaunto] + " ");
					yoko = yoko + k[kaunto];
					++kaunto;
				}else{
					System.out.println(yoko);
					tateR = tateR + yoko;
				}
			}
		} {
			int yoko = 0;
			for(int d = 0;d<(c+1);d++){
				if(d != c){
					System.out.print(k[kaunto] + " ");
					yoko = yoko + k[kaunto];
					++kaunto;
				}else{
					System.out.println(yoko);
					tateR = tateR + yoko;
				}
			}
		} int yoko = 0; yoko 0 for(int d = 0;d<(c+1);d++){
				if(d != c){
					System.out.print(k[kaunto] + " ");
					yoko = yoko + k[kaunto];
					++kaunto;
				}else{
					System.out.println(yoko);
					tateR = tateR + yoko;
				}
			} int d = 0; int d = 0; d 0 d<(c+1) d (c+1) c 1 d++ d++ d {
				if(d != c){
					System.out.print(k[kaunto] + " ");
					yoko = yoko + k[kaunto];
					++kaunto;
				}else{
					System.out.println(yoko);
					tateR = tateR + yoko;
				}
			} {
				if(d != c){
					System.out.print(k[kaunto] + " ");
					yoko = yoko + k[kaunto];
					++kaunto;
				}else{
					System.out.println(yoko);
					tateR = tateR + yoko;
				}
			} if(d != c){
					System.out.print(k[kaunto] + " ");
					yoko = yoko + k[kaunto];
					++kaunto;
				}else{
					System.out.println(yoko);
					tateR = tateR + yoko;
				} d != c d c {
					System.out.print(k[kaunto] + " ");
					yoko = yoko + k[kaunto];
					++kaunto;
				} System.out.print(k[kaunto] + " ") System.out.print System.out System System.out k[kaunto] + " " k[kaunto] k kaunto " " yoko = yoko + k[kaunto] yoko yoko + k[kaunto] yoko k[kaunto] k kaunto ++kaunto kaunto {
					System.out.println(yoko);
					tateR = tateR + yoko;
				} System.out.println(yoko) System.out.println System.out System System.out yoko tateR = tateR + yoko tateR tateR + yoko tateR yoko for(int e = 0;e<(c+1);e++){
			if(e != c){
				for(int f = 0;f<r*c;f++){
					if(f % c == e){
						tate = tate + k[f];
					}
				}
				System.out.print(tate + " ");
				tate = 0;
			}else{
				System.out.println(tateR);
			}
		} int e = 0; int e = 0; e 0 e<(c+1) e (c+1) c 1 e++ e++ e {
			if(e != c){
				for(int f = 0;f<r*c;f++){
					if(f % c == e){
						tate = tate + k[f];
					}
				}
				System.out.print(tate + " ");
				tate = 0;
			}else{
				System.out.println(tateR);
			}
		} {
			if(e != c){
				for(int f = 0;f<r*c;f++){
					if(f % c == e){
						tate = tate + k[f];
					}
				}
				System.out.print(tate + " ");
				tate = 0;
			}else{
				System.out.println(tateR);
			}
		} if(e != c){
				for(int f = 0;f<r*c;f++){
					if(f % c == e){
						tate = tate + k[f];
					}
				}
				System.out.print(tate + " ");
				tate = 0;
			}else{
				System.out.println(tateR);
			} e != c e c {
				for(int f = 0;f<r*c;f++){
					if(f % c == e){
						tate = tate + k[f];
					}
				}
				System.out.print(tate + " ");
				tate = 0;
			} for(int f = 0;f<r*c;f++){
					if(f % c == e){
						tate = tate + k[f];
					}
				} int f = 0; int f = 0; f 0 f<r*c f r*c r c f++ f++ f {
					if(f % c == e){
						tate = tate + k[f];
					}
				} {
					if(f % c == e){
						tate = tate + k[f];
					}
				} if(f % c == e){
						tate = tate + k[f];
					} f % c == e f % c f c e {
						tate = tate + k[f];
					} tate = tate + k[f] tate tate + k[f] tate k[f] k f System.out.print(tate + " ") System.out.print System.out System System.out tate + " " tate " " tate = 0 tate 0 {
				System.out.println(tateR);
			} System.out.println(tateR) System.out.println System.out System System.out tateR String[] args args public class Main {
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int k[] = new int[(r+1)*(c+1)];
		for(int a = 0;a<r*c;a++){
			k[a] = scan.nextInt();
		}
		int kaunto = 0;
		int tate = 0;
		int tateR = 0;
		for(int b = 0;b<r;b++){
			int yoko = 0;
			for(int d = 0;d<(c+1);d++){
				if(d != c){
					System.out.print(k[kaunto] + " ");
					yoko = yoko + k[kaunto];
					++kaunto;
				}else{
					System.out.println(yoko);
					tateR = tateR + yoko;
				}
			}
		}
		for(int e = 0;e<(c+1);e++){
			if(e != c){
				for(int f = 0;f<r*c;f++){
					if(f % c == e){
						tate = tate + k[f];
					}
				}
				System.out.print(tate + " ");
				tate = 0;
			}else{
				System.out.println(tateR);
			}
		}
	}
} public class Main {
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int k[] = new int[(r+1)*(c+1)];
		for(int a = 0;a<r*c;a++){
			k[a] = scan.nextInt();
		}
		int kaunto = 0;
		int tate = 0;
		int tateR = 0;
		for(int b = 0;b<r;b++){
			int yoko = 0;
			for(int d = 0;d<(c+1);d++){
				if(d != c){
					System.out.print(k[kaunto] + " ");
					yoko = yoko + k[kaunto];
					++kaunto;
				}else{
					System.out.println(yoko);
					tateR = tateR + yoko;
				}
			}
		}
		for(int e = 0;e<(c+1);e++){
			if(e != c){
				for(int f = 0;f<r*c;f++){
					if(f % c == e){
						tate = tate + k[f];
					}
				}
				System.out.print(tate + " ");
				tate = 0;
			}else{
				System.out.println(tateR);
			}
		}
	}
} Main
import java.util.Scanner;
public class Main {
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int k[] = new int[(r+1)*(c+1)];
		for(int a = 0;a<r*c;a++){
			k[a] = scan.nextInt();
		}
		int kaunto = 0;
		int tate = 0;
		int tateR = 0;
		for(int b = 0;b<r;b++){
			int yoko = 0;
			for(int d = 0;d<(c+1);d++){
				if(d != c){
					System.out.print(k[kaunto] + " ");
					yoko = yoko + k[kaunto];
					++kaunto;
				}else{
					System.out.println(yoko);
					tateR = tateR + yoko;
				}
			}
		}
		for(int e = 0;e<(c+1);e++){
			if(e != c){
				for(int f = 0;f<r*c;f++){
					if(f % c == e){
						tate = tate + k[f];
					}
				}
				System.out.print(tate + " ");
				tate = 0;
			}else{
				System.out.println(tateR);
			}
		}
	}
}