import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
	
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);

		final int r = sc.nextInt();
		final int c = sc.nextInt();
		
		int[][] matrix = new int[r + 1][c + 1];
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				matrix[i][j] = sc.nextInt();
			}
		}
		
		for(int i = 0; i < r; i++){
			
			int r_sum = 0;
			for(int j = 0; j < c; j++){
				r_sum += matrix[i][j];
			}
			
			matrix[i][c] = r_sum;
		}
		
		for(int j = 0; j <= c; j++){
			
			int c_sum = 0;
			for(int i = 0; i < r; i++){
				c_sum += matrix[i][j];
			}
			
			matrix[r][j] = c_sum;
		}
		
		for(int i = 0; i <= r; i++){
			for(int j = 0; j <= c; j++){
				if(j != 0){
					System.out.print(" ");
				}
				System.out.print(matrix[i][j]);
			}
			System.out.println();
		}
	}

} import java.util.Arrays; Arrays java.util import java.util.HashMap; HashMap java.util import java.util.Map; Map java.util import java.util.Scanner; Scanner java.util public class Main {
	
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);

		final int r = sc.nextInt();
		final int c = sc.nextInt();
		
		int[][] matrix = new int[r + 1][c + 1];
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				matrix[i][j] = sc.nextInt();
			}
		}
		
		for(int i = 0; i < r; i++){
			
			int r_sum = 0;
			for(int j = 0; j < c; j++){
				r_sum += matrix[i][j];
			}
			
			matrix[i][c] = r_sum;
		}
		
		for(int j = 0; j <= c; j++){
			
			int c_sum = 0;
			for(int i = 0; i < r; i++){
				c_sum += matrix[i][j];
			}
			
			matrix[r][j] = c_sum;
		}
		
		for(int i = 0; i <= r; i++){
			for(int j = 0; j <= c; j++){
				if(j != 0){
					System.out.print(" ");
				}
				System.out.print(matrix[i][j]);
			}
			System.out.println();
		}
	}

} Main public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);

		final int r = sc.nextInt();
		final int c = sc.nextInt();
		
		int[][] matrix = new int[r + 1][c + 1];
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				matrix[i][j] = sc.nextInt();
			}
		}
		
		for(int i = 0; i < r; i++){
			
			int r_sum = 0;
			for(int j = 0; j < c; j++){
				r_sum += matrix[i][j];
			}
			
			matrix[i][c] = r_sum;
		}
		
		for(int j = 0; j <= c; j++){
			
			int c_sum = 0;
			for(int i = 0; i < r; i++){
				c_sum += matrix[i][j];
			}
			
			matrix[r][j] = c_sum;
		}
		
		for(int i = 0; i <= r; i++){
			for(int j = 0; j <= c; j++){
				if(j != 0){
					System.out.print(" ");
				}
				System.out.print(matrix[i][j]);
			}
			System.out.println();
		}
	} main {
		Scanner sc = new Scanner(System.in);

		final int r = sc.nextInt();
		final int c = sc.nextInt();
		
		int[][] matrix = new int[r + 1][c + 1];
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				matrix[i][j] = sc.nextInt();
			}
		}
		
		for(int i = 0; i < r; i++){
			
			int r_sum = 0;
			for(int j = 0; j < c; j++){
				r_sum += matrix[i][j];
			}
			
			matrix[i][c] = r_sum;
		}
		
		for(int j = 0; j <= c; j++){
			
			int c_sum = 0;
			for(int i = 0; i < r; i++){
				c_sum += matrix[i][j];
			}
			
			matrix[r][j] = c_sum;
		}
		
		for(int i = 0; i <= r; i++){
			for(int j = 0; j <= c; j++){
				if(j != 0){
					System.out.print(" ");
				}
				System.out.print(matrix[i][j]);
			}
			System.out.println();
		}
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) final int r = sc.nextInt(); r sc.nextInt() sc.nextInt sc final int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int[][] matrix = new int[r + 1][c + 1]; matrix new int[r + 1][c + 1] r + 1 r 1 c + 1 c 1 for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				matrix[i][j] = sc.nextInt();
			}
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			for(int j = 0; j < c; j++){
				matrix[i][j] = sc.nextInt();
			}
		} {
			for(int j = 0; j < c; j++){
				matrix[i][j] = sc.nextInt();
			}
		} for(int j = 0; j < c; j++){
				matrix[i][j] = sc.nextInt();
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				matrix[i][j] = sc.nextInt();
			} {
				matrix[i][j] = sc.nextInt();
			} matrix[i][j] = sc.nextInt() matrix[i][j] matrix[i] matrix i j sc.nextInt() sc.nextInt sc for(int i = 0; i < r; i++){
			
			int r_sum = 0;
			for(int j = 0; j < c; j++){
				r_sum += matrix[i][j];
			}
			
			matrix[i][c] = r_sum;
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			
			int r_sum = 0;
			for(int j = 0; j < c; j++){
				r_sum += matrix[i][j];
			}
			
			matrix[i][c] = r_sum;
		} {
			
			int r_sum = 0;
			for(int j = 0; j < c; j++){
				r_sum += matrix[i][j];
			}
			
			matrix[i][c] = r_sum;
		} int r_sum = 0; r_sum 0 for(int j = 0; j < c; j++){
				r_sum += matrix[i][j];
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				r_sum += matrix[i][j];
			} {
				r_sum += matrix[i][j];
			} r_sum += matrix[i][j] r_sum matrix[i][j] matrix[i] matrix i j matrix[i][c] = r_sum matrix[i][c] matrix[i] matrix i c r_sum for(int j = 0; j <= c; j++){
			
			int c_sum = 0;
			for(int i = 0; i < r; i++){
				c_sum += matrix[i][j];
			}
			
			matrix[r][j] = c_sum;
		} int j = 0; int j = 0; j 0 j <= c j c j++ j++ j {
			
			int c_sum = 0;
			for(int i = 0; i < r; i++){
				c_sum += matrix[i][j];
			}
			
			matrix[r][j] = c_sum;
		} {
			
			int c_sum = 0;
			for(int i = 0; i < r; i++){
				c_sum += matrix[i][j];
			}
			
			matrix[r][j] = c_sum;
		} int c_sum = 0; c_sum 0 for(int i = 0; i < r; i++){
				c_sum += matrix[i][j];
			} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
				c_sum += matrix[i][j];
			} {
				c_sum += matrix[i][j];
			} c_sum += matrix[i][j] c_sum matrix[i][j] matrix[i] matrix i j matrix[r][j] = c_sum matrix[r][j] matrix[r] matrix r j c_sum for(int i = 0; i <= r; i++){
			for(int j = 0; j <= c; j++){
				if(j != 0){
					System.out.print(" ");
				}
				System.out.print(matrix[i][j]);
			}
			System.out.println();
		} int i = 0; int i = 0; i 0 i <= r i r i++ i++ i {
			for(int j = 0; j <= c; j++){
				if(j != 0){
					System.out.print(" ");
				}
				System.out.print(matrix[i][j]);
			}
			System.out.println();
		} {
			for(int j = 0; j <= c; j++){
				if(j != 0){
					System.out.print(" ");
				}
				System.out.print(matrix[i][j]);
			}
			System.out.println();
		} for(int j = 0; j <= c; j++){
				if(j != 0){
					System.out.print(" ");
				}
				System.out.print(matrix[i][j]);
			} int j = 0; int j = 0; j 0 j <= c j c j++ j++ j {
				if(j != 0){
					System.out.print(" ");
				}
				System.out.print(matrix[i][j]);
			} {
				if(j != 0){
					System.out.print(" ");
				}
				System.out.print(matrix[i][j]);
			} if(j != 0){
					System.out.print(" ");
				} j != 0 j 0 {
					System.out.print(" ");
				} System.out.print(" ") System.out.print System.out System System.out " " System.out.print(matrix[i][j]) System.out.print System.out System System.out matrix[i][j] matrix[i] matrix i j System.out.println() System.out.println System.out System System.out String args[] args public class Main {
	
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);

		final int r = sc.nextInt();
		final int c = sc.nextInt();
		
		int[][] matrix = new int[r + 1][c + 1];
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				matrix[i][j] = sc.nextInt();
			}
		}
		
		for(int i = 0; i < r; i++){
			
			int r_sum = 0;
			for(int j = 0; j < c; j++){
				r_sum += matrix[i][j];
			}
			
			matrix[i][c] = r_sum;
		}
		
		for(int j = 0; j <= c; j++){
			
			int c_sum = 0;
			for(int i = 0; i < r; i++){
				c_sum += matrix[i][j];
			}
			
			matrix[r][j] = c_sum;
		}
		
		for(int i = 0; i <= r; i++){
			for(int j = 0; j <= c; j++){
				if(j != 0){
					System.out.print(" ");
				}
				System.out.print(matrix[i][j]);
			}
			System.out.println();
		}
	}

} public class Main {
	
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);

		final int r = sc.nextInt();
		final int c = sc.nextInt();
		
		int[][] matrix = new int[r + 1][c + 1];
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				matrix[i][j] = sc.nextInt();
			}
		}
		
		for(int i = 0; i < r; i++){
			
			int r_sum = 0;
			for(int j = 0; j < c; j++){
				r_sum += matrix[i][j];
			}
			
			matrix[i][c] = r_sum;
		}
		
		for(int j = 0; j <= c; j++){
			
			int c_sum = 0;
			for(int i = 0; i < r; i++){
				c_sum += matrix[i][j];
			}
			
			matrix[r][j] = c_sum;
		}
		
		for(int i = 0; i <= r; i++){
			for(int j = 0; j <= c; j++){
				if(j != 0){
					System.out.print(" ");
				}
				System.out.print(matrix[i][j]);
			}
			System.out.println();
		}
	}

} Main
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
	
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);

		final int r = sc.nextInt();
		final int c = sc.nextInt();
		
		int[][] matrix = new int[r + 1][c + 1];
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				matrix[i][j] = sc.nextInt();
			}
		}
		
		for(int i = 0; i < r; i++){
			
			int r_sum = 0;
			for(int j = 0; j < c; j++){
				r_sum += matrix[i][j];
			}
			
			matrix[i][c] = r_sum;
		}
		
		for(int j = 0; j <= c; j++){
			
			int c_sum = 0;
			for(int i = 0; i < r; i++){
				c_sum += matrix[i][j];
			}
			
			matrix[r][j] = c_sum;
		}
		
		for(int i = 0; i <= r; i++){
			for(int j = 0; j <= c; j++){
				if(j != 0){
					System.out.print(" ");
				}
				System.out.print(matrix[i][j]);
			}
			System.out.println();
		}
	}

}