import java.util.Scanner;

public class Main {
	public static void main(String [] args){
		Scanner scan = new Scanner(System.in);
		int one[][], i, j;
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		one = new int[r+1][c+1]; 
		
		for(i=0; i<=r; i++){
			for(j=0; j<=c; j++){		
				one[i][j] = 0;
			}
		}
		
		for(i=0; i<r; i++){
			for(j=0; j<c; j++){		
				int x = scan.nextInt();
				one[i][j] = x;
			}
		}
		
		for(i=0; i<r; i++){
			int sumx=0;
			for(j=0; j<=c; j++){
				if(j==c){
					one[i][c] = sumx;
				}else{
					sumx += one[i][j];
				}
			}
		}
		
		for(i=0; i<=c; i++){ 
			int sumy=0;
			for(j=0; j<=r; j++){
				if(j==r){
					one[r][i] = sumy;
				}else{
					sumy += one[j][i];
				}
			}
		}
		
		
		for(i=0; i<=r; i++){
			for(j=0; j<=c; j++){
				System.out.print(one[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String [] args){
		Scanner scan = new Scanner(System.in);
		int one[][], i, j;
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		one = new int[r+1][c+1]; 
		
		for(i=0; i<=r; i++){
			for(j=0; j<=c; j++){		
				one[i][j] = 0;
			}
		}
		
		for(i=0; i<r; i++){
			for(j=0; j<c; j++){		
				int x = scan.nextInt();
				one[i][j] = x;
			}
		}
		
		for(i=0; i<r; i++){
			int sumx=0;
			for(j=0; j<=c; j++){
				if(j==c){
					one[i][c] = sumx;
				}else{
					sumx += one[i][j];
				}
			}
		}
		
		for(i=0; i<=c; i++){ 
			int sumy=0;
			for(j=0; j<=r; j++){
				if(j==r){
					one[r][i] = sumy;
				}else{
					sumy += one[j][i];
				}
			}
		}
		
		
		for(i=0; i<=r; i++){
			for(j=0; j<=c; j++){
				System.out.print(one[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
} Main public static void main(String [] args){
		Scanner scan = new Scanner(System.in);
		int one[][], i, j;
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		one = new int[r+1][c+1]; 
		
		for(i=0; i<=r; i++){
			for(j=0; j<=c; j++){		
				one[i][j] = 0;
			}
		}
		
		for(i=0; i<r; i++){
			for(j=0; j<c; j++){		
				int x = scan.nextInt();
				one[i][j] = x;
			}
		}
		
		for(i=0; i<r; i++){
			int sumx=0;
			for(j=0; j<=c; j++){
				if(j==c){
					one[i][c] = sumx;
				}else{
					sumx += one[i][j];
				}
			}
		}
		
		for(i=0; i<=c; i++){ 
			int sumy=0;
			for(j=0; j<=r; j++){
				if(j==r){
					one[r][i] = sumy;
				}else{
					sumy += one[j][i];
				}
			}
		}
		
		
		for(i=0; i<=r; i++){
			for(j=0; j<=c; j++){
				System.out.print(one[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	} main {
		Scanner scan = new Scanner(System.in);
		int one[][], i, j;
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		one = new int[r+1][c+1]; 
		
		for(i=0; i<=r; i++){
			for(j=0; j<=c; j++){		
				one[i][j] = 0;
			}
		}
		
		for(i=0; i<r; i++){
			for(j=0; j<c; j++){		
				int x = scan.nextInt();
				one[i][j] = x;
			}
		}
		
		for(i=0; i<r; i++){
			int sumx=0;
			for(j=0; j<=c; j++){
				if(j==c){
					one[i][c] = sumx;
				}else{
					sumx += one[i][j];
				}
			}
		}
		
		for(i=0; i<=c; i++){ 
			int sumy=0;
			for(j=0; j<=r; j++){
				if(j==r){
					one[r][i] = sumy;
				}else{
					sumy += one[j][i];
				}
			}
		}
		
		
		for(i=0; i<=r; i++){
			for(j=0; j<=c; j++){
				System.out.print(one[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int one[][] one i i j; j int r = scan.nextInt(); r scan.nextInt() scan.nextInt scan int c = scan.nextInt(); c scan.nextInt() scan.nextInt scan one = new int[r+1][c+1] one new int[r+1][c+1] r+1 r 1 c+1 c 1 for(i=0; i<=r; i++){
			for(j=0; j<=c; j++){		
				one[i][j] = 0;
			}
		} i=0; i=0 i 0 i<=r i r i++ i++ i {
			for(j=0; j<=c; j++){		
				one[i][j] = 0;
			}
		} {
			for(j=0; j<=c; j++){		
				one[i][j] = 0;
			}
		} for(j=0; j<=c; j++){		
				one[i][j] = 0;
			} j=0; j=0 j 0 j<=c j c j++ j++ j {		
				one[i][j] = 0;
			} {		
				one[i][j] = 0;
			} one[i][j] = 0 one[i][j] one[i] one i j 0 for(i=0; i<r; i++){
			for(j=0; j<c; j++){		
				int x = scan.nextInt();
				one[i][j] = x;
			}
		} i=0; i=0 i 0 i<r i r i++ i++ i {
			for(j=0; j<c; j++){		
				int x = scan.nextInt();
				one[i][j] = x;
			}
		} {
			for(j=0; j<c; j++){		
				int x = scan.nextInt();
				one[i][j] = x;
			}
		} for(j=0; j<c; j++){		
				int x = scan.nextInt();
				one[i][j] = x;
			} j=0; j=0 j 0 j<c j c j++ j++ j {		
				int x = scan.nextInt();
				one[i][j] = x;
			} {		
				int x = scan.nextInt();
				one[i][j] = x;
			} int x = scan.nextInt(); x scan.nextInt() scan.nextInt scan one[i][j] = x one[i][j] one[i] one i j x for(i=0; i<r; i++){
			int sumx=0;
			for(j=0; j<=c; j++){
				if(j==c){
					one[i][c] = sumx;
				}else{
					sumx += one[i][j];
				}
			}
		} i=0; i=0 i 0 i<r i r i++ i++ i {
			int sumx=0;
			for(j=0; j<=c; j++){
				if(j==c){
					one[i][c] = sumx;
				}else{
					sumx += one[i][j];
				}
			}
		} {
			int sumx=0;
			for(j=0; j<=c; j++){
				if(j==c){
					one[i][c] = sumx;
				}else{
					sumx += one[i][j];
				}
			}
		} int sumx=0; sumx 0 for(j=0; j<=c; j++){
				if(j==c){
					one[i][c] = sumx;
				}else{
					sumx += one[i][j];
				}
			} j=0; j=0 j 0 j<=c j c j++ j++ j {
				if(j==c){
					one[i][c] = sumx;
				}else{
					sumx += one[i][j];
				}
			} {
				if(j==c){
					one[i][c] = sumx;
				}else{
					sumx += one[i][j];
				}
			} if(j==c){
					one[i][c] = sumx;
				}else{
					sumx += one[i][j];
				} j==c j c {
					one[i][c] = sumx;
				} one[i][c] = sumx one[i][c] one[i] one i c sumx {
					sumx += one[i][j];
				} sumx += one[i][j] sumx one[i][j] one[i] one i j for(i=0; i<=c; i++){ 
			int sumy=0;
			for(j=0; j<=r; j++){
				if(j==r){
					one[r][i] = sumy;
				}else{
					sumy += one[j][i];
				}
			}
		} i=0; i=0 i 0 i<=c i c i++ i++ i { 
			int sumy=0;
			for(j=0; j<=r; j++){
				if(j==r){
					one[r][i] = sumy;
				}else{
					sumy += one[j][i];
				}
			}
		} { 
			int sumy=0;
			for(j=0; j<=r; j++){
				if(j==r){
					one[r][i] = sumy;
				}else{
					sumy += one[j][i];
				}
			}
		} int sumy=0; sumy 0 for(j=0; j<=r; j++){
				if(j==r){
					one[r][i] = sumy;
				}else{
					sumy += one[j][i];
				}
			} j=0; j=0 j 0 j<=r j r j++ j++ j {
				if(j==r){
					one[r][i] = sumy;
				}else{
					sumy += one[j][i];
				}
			} {
				if(j==r){
					one[r][i] = sumy;
				}else{
					sumy += one[j][i];
				}
			} if(j==r){
					one[r][i] = sumy;
				}else{
					sumy += one[j][i];
				} j==r j r {
					one[r][i] = sumy;
				} one[r][i] = sumy one[r][i] one[r] one r i sumy {
					sumy += one[j][i];
				} sumy += one[j][i] sumy one[j][i] one[j] one j i for(i=0; i<=r; i++){
			for(j=0; j<=c; j++){
				System.out.print(one[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		} i=0; i=0 i 0 i<=r i r i++ i++ i {
			for(j=0; j<=c; j++){
				System.out.print(one[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		} {
			for(j=0; j<=c; j++){
				System.out.print(one[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		} for(j=0; j<=c; j++){
				System.out.print(one[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			} j=0; j=0 j 0 j<=c j c j++ j++ j {
				System.out.print(one[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			} {
				System.out.print(one[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			} System.out.print(one[i][j]) System.out.print System.out System System.out one[i][j] one[i] one i j if(j != c){
					System.out.print(" ");
				} j != c j c {
					System.out.print(" ");
				} System.out.print(" ") System.out.print System.out System System.out " " System.out.println() System.out.println System.out System System.out String [] args args public class Main {
	public static void main(String [] args){
		Scanner scan = new Scanner(System.in);
		int one[][], i, j;
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		one = new int[r+1][c+1]; 
		
		for(i=0; i<=r; i++){
			for(j=0; j<=c; j++){		
				one[i][j] = 0;
			}
		}
		
		for(i=0; i<r; i++){
			for(j=0; j<c; j++){		
				int x = scan.nextInt();
				one[i][j] = x;
			}
		}
		
		for(i=0; i<r; i++){
			int sumx=0;
			for(j=0; j<=c; j++){
				if(j==c){
					one[i][c] = sumx;
				}else{
					sumx += one[i][j];
				}
			}
		}
		
		for(i=0; i<=c; i++){ 
			int sumy=0;
			for(j=0; j<=r; j++){
				if(j==r){
					one[r][i] = sumy;
				}else{
					sumy += one[j][i];
				}
			}
		}
		
		
		for(i=0; i<=r; i++){
			for(j=0; j<=c; j++){
				System.out.print(one[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
} public class Main {
	public static void main(String [] args){
		Scanner scan = new Scanner(System.in);
		int one[][], i, j;
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		one = new int[r+1][c+1]; 
		
		for(i=0; i<=r; i++){
			for(j=0; j<=c; j++){		
				one[i][j] = 0;
			}
		}
		
		for(i=0; i<r; i++){
			for(j=0; j<c; j++){		
				int x = scan.nextInt();
				one[i][j] = x;
			}
		}
		
		for(i=0; i<r; i++){
			int sumx=0;
			for(j=0; j<=c; j++){
				if(j==c){
					one[i][c] = sumx;
				}else{
					sumx += one[i][j];
				}
			}
		}
		
		for(i=0; i<=c; i++){ 
			int sumy=0;
			for(j=0; j<=r; j++){
				if(j==r){
					one[r][i] = sumy;
				}else{
					sumy += one[j][i];
				}
			}
		}
		
		
		for(i=0; i<=r; i++){
			for(j=0; j<=c; j++){
				System.out.print(one[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String [] args){
		Scanner scan = new Scanner(System.in);
		int one[][], i, j;
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		one = new int[r+1][c+1]; 
		
		for(i=0; i<=r; i++){
			for(j=0; j<=c; j++){		
				one[i][j] = 0;
			}
		}
		
		for(i=0; i<r; i++){
			for(j=0; j<c; j++){		
				int x = scan.nextInt();
				one[i][j] = x;
			}
		}
		
		for(i=0; i<r; i++){
			int sumx=0;
			for(j=0; j<=c; j++){
				if(j==c){
					one[i][c] = sumx;
				}else{
					sumx += one[i][j];
				}
			}
		}
		
		for(i=0; i<=c; i++){ 
			int sumy=0;
			for(j=0; j<=r; j++){
				if(j==r){
					one[r][i] = sumy;
				}else{
					sumy += one[j][i];
				}
			}
		}
		
		
		for(i=0; i<=r; i++){
			for(j=0; j<=c; j++){
				System.out.print(one[i][j]);
				if(j != c){
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
}