
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {



		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String[] imput = br.readLine().split(" ");
		int r = Integer.parseInt(imput[0]);
		int c = Integer.parseInt(imput[1]);
		int[][] data = new int[r+1][c+1];
		int[] sumR = new int[c];
		int[] sumC = new int[r];
		int sumX = 0;
		int sumA =0;
		int cnt=0;

		for(int i=0;i<r;i++){
			String[] str = br.readLine().split(" ");
			for(int j=0;j<c;j++){
				//System.out.println(str[j]);
				data[i][j] = Integer.parseInt(str[j]);

			}
		}

		for(int i=0;i<r;i++){
			
			for(int j=0;j<c;j++){
				sumA += data[i][j];
				sumX += data[i][j];
				//System.out.println(sumX);
				if(j == c-1){
					data[i][c]=sumX;
					sumX=0;
				}
			}//System.out.print(data[r][i]+" ");
			
			for(int j=0;j<c;j++){
				
				data[r][j] += data[i][j];
				
				
			}
			
		}
		
		//System.out.println(sumA);
		
		for(int i=0;i<r+1;i++){
			for(int j=0;j<c+1;j++){
				
				if(i==r&&j==c){
					System.out.print(sumA);
				}else{
					System.out.print(data[i][j]);
					if(j!=c){
						System.out.print(" ");
					}
				}

			}System.out.println();
		}

	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {

	public static void main(String[] args) throws IOException {



		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String[] imput = br.readLine().split(" ");
		int r = Integer.parseInt(imput[0]);
		int c = Integer.parseInt(imput[1]);
		int[][] data = new int[r+1][c+1];
		int[] sumR = new int[c];
		int[] sumC = new int[r];
		int sumX = 0;
		int sumA =0;
		int cnt=0;

		for(int i=0;i<r;i++){
			String[] str = br.readLine().split(" ");
			for(int j=0;j<c;j++){
				//System.out.println(str[j]);
				data[i][j] = Integer.parseInt(str[j]);

			}
		}

		for(int i=0;i<r;i++){
			
			for(int j=0;j<c;j++){
				sumA += data[i][j];
				sumX += data[i][j];
				//System.out.println(sumX);
				if(j == c-1){
					data[i][c]=sumX;
					sumX=0;
				}
			}//System.out.print(data[r][i]+" ");
			
			for(int j=0;j<c;j++){
				
				data[r][j] += data[i][j];
				
				
			}
			
		}
		
		//System.out.println(sumA);
		
		for(int i=0;i<r+1;i++){
			for(int j=0;j<c+1;j++){
				
				if(i==r&&j==c){
					System.out.print(sumA);
				}else{
					System.out.print(data[i][j]);
					if(j!=c){
						System.out.print(" ");
					}
				}

			}System.out.println();
		}

	}
} Main public static void main(String[] args) throws IOException {



		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String[] imput = br.readLine().split(" ");
		int r = Integer.parseInt(imput[0]);
		int c = Integer.parseInt(imput[1]);
		int[][] data = new int[r+1][c+1];
		int[] sumR = new int[c];
		int[] sumC = new int[r];
		int sumX = 0;
		int sumA =0;
		int cnt=0;

		for(int i=0;i<r;i++){
			String[] str = br.readLine().split(" ");
			for(int j=0;j<c;j++){
				//System.out.println(str[j]);
				data[i][j] = Integer.parseInt(str[j]);

			}
		}

		for(int i=0;i<r;i++){
			
			for(int j=0;j<c;j++){
				sumA += data[i][j];
				sumX += data[i][j];
				//System.out.println(sumX);
				if(j == c-1){
					data[i][c]=sumX;
					sumX=0;
				}
			}//System.out.print(data[r][i]+" ");
			
			for(int j=0;j<c;j++){
				
				data[r][j] += data[i][j];
				
				
			}
			
		}
		
		//System.out.println(sumA);
		
		for(int i=0;i<r+1;i++){
			for(int j=0;j<c+1;j++){
				
				if(i==r&&j==c){
					System.out.print(sumA);
				}else{
					System.out.print(data[i][j]);
					if(j!=c){
						System.out.print(" ");
					}
				}

			}System.out.println();
		}

	} main {



		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String[] imput = br.readLine().split(" ");
		int r = Integer.parseInt(imput[0]);
		int c = Integer.parseInt(imput[1]);
		int[][] data = new int[r+1][c+1];
		int[] sumR = new int[c];
		int[] sumC = new int[r];
		int sumX = 0;
		int sumA =0;
		int cnt=0;

		for(int i=0;i<r;i++){
			String[] str = br.readLine().split(" ");
			for(int j=0;j<c;j++){
				//System.out.println(str[j]);
				data[i][j] = Integer.parseInt(str[j]);

			}
		}

		for(int i=0;i<r;i++){
			
			for(int j=0;j<c;j++){
				sumA += data[i][j];
				sumX += data[i][j];
				//System.out.println(sumX);
				if(j == c-1){
					data[i][c]=sumX;
					sumX=0;
				}
			}//System.out.print(data[r][i]+" ");
			
			for(int j=0;j<c;j++){
				
				data[r][j] += data[i][j];
				
				
			}
			
		}
		
		//System.out.println(sumA);
		
		for(int i=0;i<r+1;i++){
			for(int j=0;j<c+1;j++){
				
				if(i==r&&j==c){
					System.out.print(sumA);
				}else{
					System.out.print(data[i][j]);
					if(j!=c){
						System.out.print(" ");
					}
				}

			}System.out.println();
		}

	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] imput = br.readLine().split(" "); imput br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int r = Integer.parseInt(imput[0]); r Integer.parseInt(imput[0]) Integer.parseInt Integer imput[0] imput 0 int c = Integer.parseInt(imput[1]); c Integer.parseInt(imput[1]) Integer.parseInt Integer imput[1] imput 1 int[][] data = new int[r+1][c+1]; data new int[r+1][c+1] r+1 r 1 c+1 c 1 int[] sumR = new int[c]; sumR new int[c] c int[] sumC = new int[r]; sumC new int[r] r int sumX = 0; sumX 0 int sumA =0; sumA 0 int cnt=0; cnt 0 for(int i=0;i<r;i++){
			String[] str = br.readLine().split(" ");
			for(int j=0;j<c;j++){
				//System.out.println(str[j]);
				data[i][j] = Integer.parseInt(str[j]);

			}
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			String[] str = br.readLine().split(" ");
			for(int j=0;j<c;j++){
				//System.out.println(str[j]);
				data[i][j] = Integer.parseInt(str[j]);

			}
		} {
			String[] str = br.readLine().split(" ");
			for(int j=0;j<c;j++){
				//System.out.println(str[j]);
				data[i][j] = Integer.parseInt(str[j]);

			}
		} String[] str = br.readLine().split(" "); str br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " for(int j=0;j<c;j++){
				//System.out.println(str[j]);
				data[i][j] = Integer.parseInt(str[j]);

			} int j=0; int j=0; j 0 j<c j c j++ j++ j {
				//System.out.println(str[j]);
				data[i][j] = Integer.parseInt(str[j]);

			} {
				//System.out.println(str[j]);
				data[i][j] = Integer.parseInt(str[j]);

			} data[i][j] = Integer.parseInt(str[j]) data[i][j] data[i] data i j Integer.parseInt(str[j]) Integer.parseInt Integer str[j] str j for(int i=0;i<r;i++){
			
			for(int j=0;j<c;j++){
				sumA += data[i][j];
				sumX += data[i][j];
				//System.out.println(sumX);
				if(j == c-1){
					data[i][c]=sumX;
					sumX=0;
				}
			}//System.out.print(data[r][i]+" ");
			
			for(int j=0;j<c;j++){
				
				data[r][j] += data[i][j];
				
				
			}
			
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			
			for(int j=0;j<c;j++){
				sumA += data[i][j];
				sumX += data[i][j];
				//System.out.println(sumX);
				if(j == c-1){
					data[i][c]=sumX;
					sumX=0;
				}
			}//System.out.print(data[r][i]+" ");
			
			for(int j=0;j<c;j++){
				
				data[r][j] += data[i][j];
				
				
			}
			
		} {
			
			for(int j=0;j<c;j++){
				sumA += data[i][j];
				sumX += data[i][j];
				//System.out.println(sumX);
				if(j == c-1){
					data[i][c]=sumX;
					sumX=0;
				}
			}//System.out.print(data[r][i]+" ");
			
			for(int j=0;j<c;j++){
				
				data[r][j] += data[i][j];
				
				
			}
			
		} for(int j=0;j<c;j++){
				sumA += data[i][j];
				sumX += data[i][j];
				//System.out.println(sumX);
				if(j == c-1){
					data[i][c]=sumX;
					sumX=0;
				}
			}//System.out.print(data[r][i]+" "); int j=0; int j=0; j 0 j<c j c j++ j++ j {
				sumA += data[i][j];
				sumX += data[i][j];
				//System.out.println(sumX);
				if(j == c-1){
					data[i][c]=sumX;
					sumX=0;
				}
			}//System.out.print(data[r][i]+" "); {
				sumA += data[i][j];
				sumX += data[i][j];
				//System.out.println(sumX);
				if(j == c-1){
					data[i][c]=sumX;
					sumX=0;
				}
			} sumA += data[i][j] sumA data[i][j] data[i] data i j sumX += data[i][j] sumX data[i][j] data[i] data i j if(j == c-1){
					data[i][c]=sumX;
					sumX=0;
				} j == c-1 j c-1 c 1 {
					data[i][c]=sumX;
					sumX=0;
				} data[i][c]=sumX data[i][c] data[i] data i c sumX sumX=0 sumX 0 for(int j=0;j<c;j++){
				
				data[r][j] += data[i][j];
				
				
			} int j=0; int j=0; j 0 j<c j c j++ j++ j {
				
				data[r][j] += data[i][j];
				
				
			} {
				
				data[r][j] += data[i][j];
				
				
			} data[r][j] += data[i][j] data[r][j] data[r] data r j data[i][j] data[i] data i j for(int i=0;i<r+1;i++){
			for(int j=0;j<c+1;j++){
				
				if(i==r&&j==c){
					System.out.print(sumA);
				}else{
					System.out.print(data[i][j]);
					if(j!=c){
						System.out.print(" ");
					}
				}

			}System.out.println();
		} int i=0; int i=0; i 0 i<r+1 i r+1 r 1 i++ i++ i {
			for(int j=0;j<c+1;j++){
				
				if(i==r&&j==c){
					System.out.print(sumA);
				}else{
					System.out.print(data[i][j]);
					if(j!=c){
						System.out.print(" ");
					}
				}

			}System.out.println();
		} {
			for(int j=0;j<c+1;j++){
				
				if(i==r&&j==c){
					System.out.print(sumA);
				}else{
					System.out.print(data[i][j]);
					if(j!=c){
						System.out.print(" ");
					}
				}

			}System.out.println();
		} for(int j=0;j<c+1;j++){
				
				if(i==r&&j==c){
					System.out.print(sumA);
				}else{
					System.out.print(data[i][j]);
					if(j!=c){
						System.out.print(" ");
					}
				}

			} int j=0; int j=0; j 0 j<c+1 j c+1 c 1 j++ j++ j {
				
				if(i==r&&j==c){
					System.out.print(sumA);
				}else{
					System.out.print(data[i][j]);
					if(j!=c){
						System.out.print(" ");
					}
				}

			} {
				
				if(i==r&&j==c){
					System.out.print(sumA);
				}else{
					System.out.print(data[i][j]);
					if(j!=c){
						System.out.print(" ");
					}
				}

			} if(i==r&&j==c){
					System.out.print(sumA);
				}else{
					System.out.print(data[i][j]);
					if(j!=c){
						System.out.print(" ");
					}
				} i==r&&j==c i==r i r j==c j c {
					System.out.print(sumA);
				} System.out.print(sumA) System.out.print System.out System System.out sumA {
					System.out.print(data[i][j]);
					if(j!=c){
						System.out.print(" ");
					}
				} System.out.print(data[i][j]) System.out.print System.out System System.out data[i][j] data[i] data 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) throws IOException {



		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String[] imput = br.readLine().split(" ");
		int r = Integer.parseInt(imput[0]);
		int c = Integer.parseInt(imput[1]);
		int[][] data = new int[r+1][c+1];
		int[] sumR = new int[c];
		int[] sumC = new int[r];
		int sumX = 0;
		int sumA =0;
		int cnt=0;

		for(int i=0;i<r;i++){
			String[] str = br.readLine().split(" ");
			for(int j=0;j<c;j++){
				//System.out.println(str[j]);
				data[i][j] = Integer.parseInt(str[j]);

			}
		}

		for(int i=0;i<r;i++){
			
			for(int j=0;j<c;j++){
				sumA += data[i][j];
				sumX += data[i][j];
				//System.out.println(sumX);
				if(j == c-1){
					data[i][c]=sumX;
					sumX=0;
				}
			}//System.out.print(data[r][i]+" ");
			
			for(int j=0;j<c;j++){
				
				data[r][j] += data[i][j];
				
				
			}
			
		}
		
		//System.out.println(sumA);
		
		for(int i=0;i<r+1;i++){
			for(int j=0;j<c+1;j++){
				
				if(i==r&&j==c){
					System.out.print(sumA);
				}else{
					System.out.print(data[i][j]);
					if(j!=c){
						System.out.print(" ");
					}
				}

			}System.out.println();
		}

	}
} public class Main {

	public static void main(String[] args) throws IOException {



		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String[] imput = br.readLine().split(" ");
		int r = Integer.parseInt(imput[0]);
		int c = Integer.parseInt(imput[1]);
		int[][] data = new int[r+1][c+1];
		int[] sumR = new int[c];
		int[] sumC = new int[r];
		int sumX = 0;
		int sumA =0;
		int cnt=0;

		for(int i=0;i<r;i++){
			String[] str = br.readLine().split(" ");
			for(int j=0;j<c;j++){
				//System.out.println(str[j]);
				data[i][j] = Integer.parseInt(str[j]);

			}
		}

		for(int i=0;i<r;i++){
			
			for(int j=0;j<c;j++){
				sumA += data[i][j];
				sumX += data[i][j];
				//System.out.println(sumX);
				if(j == c-1){
					data[i][c]=sumX;
					sumX=0;
				}
			}//System.out.print(data[r][i]+" ");
			
			for(int j=0;j<c;j++){
				
				data[r][j] += data[i][j];
				
				
			}
			
		}
		
		//System.out.println(sumA);
		
		for(int i=0;i<r+1;i++){
			for(int j=0;j<c+1;j++){
				
				if(i==r&&j==c){
					System.out.print(sumA);
				}else{
					System.out.print(data[i][j]);
					if(j!=c){
						System.out.print(" ");
					}
				}

			}System.out.println();
		}

	}
} Main

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {



		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String[] imput = br.readLine().split(" ");
		int r = Integer.parseInt(imput[0]);
		int c = Integer.parseInt(imput[1]);
		int[][] data = new int[r+1][c+1];
		int[] sumR = new int[c];
		int[] sumC = new int[r];
		int sumX = 0;
		int sumA =0;
		int cnt=0;

		for(int i=0;i<r;i++){
			String[] str = br.readLine().split(" ");
			for(int j=0;j<c;j++){
				//System.out.println(str[j]);
				data[i][j] = Integer.parseInt(str[j]);

			}
		}

		for(int i=0;i<r;i++){
			
			for(int j=0;j<c;j++){
				sumA += data[i][j];
				sumX += data[i][j];
				//System.out.println(sumX);
				if(j == c-1){
					data[i][c]=sumX;
					sumX=0;
				}
			}//System.out.print(data[r][i]+" ");
			
			for(int j=0;j<c;j++){
				
				data[r][j] += data[i][j];
				
				
			}
			
		}
		
		//System.out.println(sumA);
		
		for(int i=0;i<r+1;i++){
			for(int j=0;j<c+1;j++){
				
				if(i==r&&j==c){
					System.out.print(sumA);
				}else{
					System.out.print(data[i][j]);
					if(j!=c){
						System.out.print(" ");
					}
				}

			}System.out.println();
		}

	}
}