import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????
		Scanner scan = new Scanner(System.in);
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] numbers = new int[r + 1][c + 1];
		
		int sUB = 0;
		
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				numbers[i][j] = scan.nextInt();
			}
		}
		
		for(int k = 0; k < r; k++){
			int sB = 0;
			for(int l = 0; l < c; l++){
				sB = numbers[k][l] + sB;
			}
			numbers[k][c] = sB;
		}
		
		for(int l = 0; l < c; l++){
			int sU = 0;
			for(int k = 0; k < r; k++){
				sU = numbers[k][l] + sU;
			}
			numbers[r][l] = sU;
			
			sUB = numbers[r][l] + sUB;
		}
		numbers[r][c] = sUB;
		
		for(int p = 0; p < r + 1; p++){
			for(int q = 0; q < c + 1; q++){
				System.out.print(numbers[p][q]);
				if(q == c){
					System.out.print("");
				}else{
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????
		Scanner scan = new Scanner(System.in);
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] numbers = new int[r + 1][c + 1];
		
		int sUB = 0;
		
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				numbers[i][j] = scan.nextInt();
			}
		}
		
		for(int k = 0; k < r; k++){
			int sB = 0;
			for(int l = 0; l < c; l++){
				sB = numbers[k][l] + sB;
			}
			numbers[k][c] = sB;
		}
		
		for(int l = 0; l < c; l++){
			int sU = 0;
			for(int k = 0; k < r; k++){
				sU = numbers[k][l] + sU;
			}
			numbers[r][l] = sU;
			
			sUB = numbers[r][l] + sUB;
		}
		numbers[r][c] = sUB;
		
		for(int p = 0; p < r + 1; p++){
			for(int q = 0; q < c + 1; q++){
				System.out.print(numbers[p][q]);
				if(q == c){
					System.out.print("");
				}else{
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
} Main public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????
		Scanner scan = new Scanner(System.in);
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] numbers = new int[r + 1][c + 1];
		
		int sUB = 0;
		
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				numbers[i][j] = scan.nextInt();
			}
		}
		
		for(int k = 0; k < r; k++){
			int sB = 0;
			for(int l = 0; l < c; l++){
				sB = numbers[k][l] + sB;
			}
			numbers[k][c] = sB;
		}
		
		for(int l = 0; l < c; l++){
			int sU = 0;
			for(int k = 0; k < r; k++){
				sU = numbers[k][l] + sU;
			}
			numbers[r][l] = sU;
			
			sUB = numbers[r][l] + sUB;
		}
		numbers[r][c] = sUB;
		
		for(int p = 0; p < r + 1; p++){
			for(int q = 0; q < c + 1; q++){
				System.out.print(numbers[p][q]);
				if(q == c){
					System.out.print("");
				}else{
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	} main {
		// TODO ?????????????????????????????????????????????
		Scanner scan = new Scanner(System.in);
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] numbers = new int[r + 1][c + 1];
		
		int sUB = 0;
		
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				numbers[i][j] = scan.nextInt();
			}
		}
		
		for(int k = 0; k < r; k++){
			int sB = 0;
			for(int l = 0; l < c; l++){
				sB = numbers[k][l] + sB;
			}
			numbers[k][c] = sB;
		}
		
		for(int l = 0; l < c; l++){
			int sU = 0;
			for(int k = 0; k < r; k++){
				sU = numbers[k][l] + sU;
			}
			numbers[r][l] = sU;
			
			sUB = numbers[r][l] + sUB;
		}
		numbers[r][c] = sUB;
		
		for(int p = 0; p < r + 1; p++){
			for(int q = 0; q < c + 1; q++){
				System.out.print(numbers[p][q]);
				if(q == c){
					System.out.print("");
				}else{
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	} 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[][] numbers = new int[r + 1][c + 1]; numbers new int[r + 1][c + 1] r + 1 r 1 c + 1 c 1 int sUB = 0; sUB 0 for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				numbers[i][j] = scan.nextInt();
			}
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			for(int j = 0; j < c; j++){
				numbers[i][j] = scan.nextInt();
			}
		} {
			for(int j = 0; j < c; j++){
				numbers[i][j] = scan.nextInt();
			}
		} for(int j = 0; j < c; j++){
				numbers[i][j] = scan.nextInt();
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				numbers[i][j] = scan.nextInt();
			} {
				numbers[i][j] = scan.nextInt();
			} numbers[i][j] = scan.nextInt() numbers[i][j] numbers[i] numbers i j scan.nextInt() scan.nextInt scan for(int k = 0; k < r; k++){
			int sB = 0;
			for(int l = 0; l < c; l++){
				sB = numbers[k][l] + sB;
			}
			numbers[k][c] = sB;
		} int k = 0; int k = 0; k 0 k < r k r k++ k++ k {
			int sB = 0;
			for(int l = 0; l < c; l++){
				sB = numbers[k][l] + sB;
			}
			numbers[k][c] = sB;
		} {
			int sB = 0;
			for(int l = 0; l < c; l++){
				sB = numbers[k][l] + sB;
			}
			numbers[k][c] = sB;
		} int sB = 0; sB 0 for(int l = 0; l < c; l++){
				sB = numbers[k][l] + sB;
			} int l = 0; int l = 0; l 0 l < c l c l++ l++ l {
				sB = numbers[k][l] + sB;
			} {
				sB = numbers[k][l] + sB;
			} sB = numbers[k][l] + sB sB numbers[k][l] + sB numbers[k][l] numbers[k] numbers k l sB numbers[k][c] = sB numbers[k][c] numbers[k] numbers k c sB for(int l = 0; l < c; l++){
			int sU = 0;
			for(int k = 0; k < r; k++){
				sU = numbers[k][l] + sU;
			}
			numbers[r][l] = sU;
			
			sUB = numbers[r][l] + sUB;
		} int l = 0; int l = 0; l 0 l < c l c l++ l++ l {
			int sU = 0;
			for(int k = 0; k < r; k++){
				sU = numbers[k][l] + sU;
			}
			numbers[r][l] = sU;
			
			sUB = numbers[r][l] + sUB;
		} {
			int sU = 0;
			for(int k = 0; k < r; k++){
				sU = numbers[k][l] + sU;
			}
			numbers[r][l] = sU;
			
			sUB = numbers[r][l] + sUB;
		} int sU = 0; sU 0 for(int k = 0; k < r; k++){
				sU = numbers[k][l] + sU;
			} int k = 0; int k = 0; k 0 k < r k r k++ k++ k {
				sU = numbers[k][l] + sU;
			} {
				sU = numbers[k][l] + sU;
			} sU = numbers[k][l] + sU sU numbers[k][l] + sU numbers[k][l] numbers[k] numbers k l sU numbers[r][l] = sU numbers[r][l] numbers[r] numbers r l sU sUB = numbers[r][l] + sUB sUB numbers[r][l] + sUB numbers[r][l] numbers[r] numbers r l sUB numbers[r][c] = sUB numbers[r][c] numbers[r] numbers r c sUB for(int p = 0; p < r + 1; p++){
			for(int q = 0; q < c + 1; q++){
				System.out.print(numbers[p][q]);
				if(q == c){
					System.out.print("");
				}else{
					System.out.print(" ");
				}
			}
			System.out.println();
		} int p = 0; int p = 0; p 0 p < r + 1 p r + 1 r 1 p++ p++ p {
			for(int q = 0; q < c + 1; q++){
				System.out.print(numbers[p][q]);
				if(q == c){
					System.out.print("");
				}else{
					System.out.print(" ");
				}
			}
			System.out.println();
		} {
			for(int q = 0; q < c + 1; q++){
				System.out.print(numbers[p][q]);
				if(q == c){
					System.out.print("");
				}else{
					System.out.print(" ");
				}
			}
			System.out.println();
		} for(int q = 0; q < c + 1; q++){
				System.out.print(numbers[p][q]);
				if(q == c){
					System.out.print("");
				}else{
					System.out.print(" ");
				}
			} int q = 0; int q = 0; q 0 q < c + 1 q c + 1 c 1 q++ q++ q {
				System.out.print(numbers[p][q]);
				if(q == c){
					System.out.print("");
				}else{
					System.out.print(" ");
				}
			} {
				System.out.print(numbers[p][q]);
				if(q == c){
					System.out.print("");
				}else{
					System.out.print(" ");
				}
			} System.out.print(numbers[p][q]) System.out.print System.out System System.out numbers[p][q] numbers[p] numbers p q if(q == c){
					System.out.print("");
				}else{
					System.out.print(" ");
				} q == c q c {
					System.out.print("");
				} System.out.print("") System.out.print System.out System System.out "" {
					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) {
		// TODO ?????????????????????????????????????????????
		Scanner scan = new Scanner(System.in);
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] numbers = new int[r + 1][c + 1];
		
		int sUB = 0;
		
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				numbers[i][j] = scan.nextInt();
			}
		}
		
		for(int k = 0; k < r; k++){
			int sB = 0;
			for(int l = 0; l < c; l++){
				sB = numbers[k][l] + sB;
			}
			numbers[k][c] = sB;
		}
		
		for(int l = 0; l < c; l++){
			int sU = 0;
			for(int k = 0; k < r; k++){
				sU = numbers[k][l] + sU;
			}
			numbers[r][l] = sU;
			
			sUB = numbers[r][l] + sUB;
		}
		numbers[r][c] = sUB;
		
		for(int p = 0; p < r + 1; p++){
			for(int q = 0; q < c + 1; q++){
				System.out.print(numbers[p][q]);
				if(q == c){
					System.out.print("");
				}else{
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
} public class Main {

	public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????
		Scanner scan = new Scanner(System.in);
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] numbers = new int[r + 1][c + 1];
		
		int sUB = 0;
		
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				numbers[i][j] = scan.nextInt();
			}
		}
		
		for(int k = 0; k < r; k++){
			int sB = 0;
			for(int l = 0; l < c; l++){
				sB = numbers[k][l] + sB;
			}
			numbers[k][c] = sB;
		}
		
		for(int l = 0; l < c; l++){
			int sU = 0;
			for(int k = 0; k < r; k++){
				sU = numbers[k][l] + sU;
			}
			numbers[r][l] = sU;
			
			sUB = numbers[r][l] + sUB;
		}
		numbers[r][c] = sUB;
		
		for(int p = 0; p < r + 1; p++){
			for(int q = 0; q < c + 1; q++){
				System.out.print(numbers[p][q]);
				if(q == c){
					System.out.print("");
				}else{
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????
		Scanner scan = new Scanner(System.in);
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] numbers = new int[r + 1][c + 1];
		
		int sUB = 0;
		
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				numbers[i][j] = scan.nextInt();
			}
		}
		
		for(int k = 0; k < r; k++){
			int sB = 0;
			for(int l = 0; l < c; l++){
				sB = numbers[k][l] + sB;
			}
			numbers[k][c] = sB;
		}
		
		for(int l = 0; l < c; l++){
			int sU = 0;
			for(int k = 0; k < r; k++){
				sU = numbers[k][l] + sU;
			}
			numbers[r][l] = sU;
			
			sUB = numbers[r][l] + sUB;
		}
		numbers[r][c] = sUB;
		
		for(int p = 0; p < r + 1; p++){
			for(int q = 0; q < c + 1; q++){
				System.out.print(numbers[p][q]);
				if(q == c){
					System.out.print("");
				}else{
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
}