import java.util.*;

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

		int r = sc.nextInt();
		int c = sc.nextInt();
		int tblData[][] = new int[c+1][r+1];
		
		//???????????\???
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				tblData[j][i] = sc.nextInt();
			}
		}
		
		//????????????????¨?
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				tblData[c][i] = tblData[c][i] + tblData[j][i];
			}
		}
		
		//????????????????¨?
		for(int j = 0; j < c; j++) {
			for(int i = 0; i < r; i++) {
				tblData[j][r] = tblData[j][r] + tblData[j][i];
			}
		}
		
		//???????¨?
		for(int i = 0; i < r; i++) {
			tblData[c][r] = tblData[c][r] + tblData[c][i];
		}
		
		for(int i = 0; i < r + 1; i++) {
			for(int j = 0; j < c + 1; j++) {
				if(j != c) {
					System.out.printf("%d ", tblData[j][i]);
				} else if(j == c) {
					System.out.printf("%d", tblData[j][i]);
				}
			}
			System.out.println();
		}
	}
} import java.util.*; util.* java public class Main {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);

		int r = sc.nextInt();
		int c = sc.nextInt();
		int tblData[][] = new int[c+1][r+1];
		
		//???????????\???
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				tblData[j][i] = sc.nextInt();
			}
		}
		
		//????????????????¨?
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				tblData[c][i] = tblData[c][i] + tblData[j][i];
			}
		}
		
		//????????????????¨?
		for(int j = 0; j < c; j++) {
			for(int i = 0; i < r; i++) {
				tblData[j][r] = tblData[j][r] + tblData[j][i];
			}
		}
		
		//???????¨?
		for(int i = 0; i < r; i++) {
			tblData[c][r] = tblData[c][r] + tblData[c][i];
		}
		
		for(int i = 0; i < r + 1; i++) {
			for(int j = 0; j < c + 1; j++) {
				if(j != c) {
					System.out.printf("%d ", tblData[j][i]);
				} else if(j == c) {
					System.out.printf("%d", tblData[j][i]);
				}
			}
			System.out.println();
		}
	}
} Main public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);

		int r = sc.nextInt();
		int c = sc.nextInt();
		int tblData[][] = new int[c+1][r+1];
		
		//???????????\???
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				tblData[j][i] = sc.nextInt();
			}
		}
		
		//????????????????¨?
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				tblData[c][i] = tblData[c][i] + tblData[j][i];
			}
		}
		
		//????????????????¨?
		for(int j = 0; j < c; j++) {
			for(int i = 0; i < r; i++) {
				tblData[j][r] = tblData[j][r] + tblData[j][i];
			}
		}
		
		//???????¨?
		for(int i = 0; i < r; i++) {
			tblData[c][r] = tblData[c][r] + tblData[c][i];
		}
		
		for(int i = 0; i < r + 1; i++) {
			for(int j = 0; j < c + 1; j++) {
				if(j != c) {
					System.out.printf("%d ", tblData[j][i]);
				} else if(j == c) {
					System.out.printf("%d", tblData[j][i]);
				}
			}
			System.out.println();
		}
	} main {
		
		Scanner sc = new Scanner(System.in);

		int r = sc.nextInt();
		int c = sc.nextInt();
		int tblData[][] = new int[c+1][r+1];
		
		//???????????\???
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				tblData[j][i] = sc.nextInt();
			}
		}
		
		//????????????????¨?
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				tblData[c][i] = tblData[c][i] + tblData[j][i];
			}
		}
		
		//????????????????¨?
		for(int j = 0; j < c; j++) {
			for(int i = 0; i < r; i++) {
				tblData[j][r] = tblData[j][r] + tblData[j][i];
			}
		}
		
		//???????¨?
		for(int i = 0; i < r; i++) {
			tblData[c][r] = tblData[c][r] + tblData[c][i];
		}
		
		for(int i = 0; i < r + 1; i++) {
			for(int j = 0; j < c + 1; j++) {
				if(j != c) {
					System.out.printf("%d ", tblData[j][i]);
				} else if(j == c) {
					System.out.printf("%d", tblData[j][i]);
				}
			}
			System.out.println();
		}
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int r = sc.nextInt(); r sc.nextInt() sc.nextInt sc int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int tblData[][] = new int[c+1][r+1]; tblData new int[c+1][r+1] c+1 c 1 r+1 r 1 for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				tblData[j][i] = sc.nextInt();
			}
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			for(int j = 0; j < c; j++) {
				tblData[j][i] = sc.nextInt();
			}
		} {
			for(int j = 0; j < c; j++) {
				tblData[j][i] = sc.nextInt();
			}
		} for(int j = 0; j < c; j++) {
				tblData[j][i] = sc.nextInt();
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				tblData[j][i] = sc.nextInt();
			} {
				tblData[j][i] = sc.nextInt();
			} tblData[j][i] = sc.nextInt() tblData[j][i] tblData[j] tblData j i sc.nextInt() sc.nextInt sc for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				tblData[c][i] = tblData[c][i] + tblData[j][i];
			}
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			for(int j = 0; j < c; j++) {
				tblData[c][i] = tblData[c][i] + tblData[j][i];
			}
		} {
			for(int j = 0; j < c; j++) {
				tblData[c][i] = tblData[c][i] + tblData[j][i];
			}
		} for(int j = 0; j < c; j++) {
				tblData[c][i] = tblData[c][i] + tblData[j][i];
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				tblData[c][i] = tblData[c][i] + tblData[j][i];
			} {
				tblData[c][i] = tblData[c][i] + tblData[j][i];
			} tblData[c][i] = tblData[c][i] + tblData[j][i] tblData[c][i] tblData[c] tblData c i tblData[c][i] + tblData[j][i] tblData[c][i] tblData[c] tblData c i tblData[j][i] tblData[j] tblData j i for(int j = 0; j < c; j++) {
			for(int i = 0; i < r; i++) {
				tblData[j][r] = tblData[j][r] + tblData[j][i];
			}
		} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
			for(int i = 0; i < r; i++) {
				tblData[j][r] = tblData[j][r] + tblData[j][i];
			}
		} {
			for(int i = 0; i < r; i++) {
				tblData[j][r] = tblData[j][r] + tblData[j][i];
			}
		} for(int i = 0; i < r; i++) {
				tblData[j][r] = tblData[j][r] + tblData[j][i];
			} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
				tblData[j][r] = tblData[j][r] + tblData[j][i];
			} {
				tblData[j][r] = tblData[j][r] + tblData[j][i];
			} tblData[j][r] = tblData[j][r] + tblData[j][i] tblData[j][r] tblData[j] tblData j r tblData[j][r] + tblData[j][i] tblData[j][r] tblData[j] tblData j r tblData[j][i] tblData[j] tblData j i for(int i = 0; i < r; i++) {
			tblData[c][r] = tblData[c][r] + tblData[c][i];
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			tblData[c][r] = tblData[c][r] + tblData[c][i];
		} {
			tblData[c][r] = tblData[c][r] + tblData[c][i];
		} tblData[c][r] = tblData[c][r] + tblData[c][i] tblData[c][r] tblData[c] tblData c r tblData[c][r] + tblData[c][i] tblData[c][r] tblData[c] tblData c r tblData[c][i] tblData[c] tblData c i for(int i = 0; i < r + 1; i++) {
			for(int j = 0; j < c + 1; j++) {
				if(j != c) {
					System.out.printf("%d ", tblData[j][i]);
				} else if(j == c) {
					System.out.printf("%d", tblData[j][i]);
				}
			}
			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(j != c) {
					System.out.printf("%d ", tblData[j][i]);
				} else if(j == c) {
					System.out.printf("%d", tblData[j][i]);
				}
			}
			System.out.println();
		} {
			for(int j = 0; j < c + 1; j++) {
				if(j != c) {
					System.out.printf("%d ", tblData[j][i]);
				} else if(j == c) {
					System.out.printf("%d", tblData[j][i]);
				}
			}
			System.out.println();
		} for(int j = 0; j < c + 1; j++) {
				if(j != c) {
					System.out.printf("%d ", tblData[j][i]);
				} else if(j == c) {
					System.out.printf("%d", tblData[j][i]);
				}
			} int j = 0; int j = 0; j 0 j < c + 1 j c + 1 c 1 j++ j++ j {
				if(j != c) {
					System.out.printf("%d ", tblData[j][i]);
				} else if(j == c) {
					System.out.printf("%d", tblData[j][i]);
				}
			} {
				if(j != c) {
					System.out.printf("%d ", tblData[j][i]);
				} else if(j == c) {
					System.out.printf("%d", tblData[j][i]);
				}
			} if(j != c) {
					System.out.printf("%d ", tblData[j][i]);
				} else if(j == c) {
					System.out.printf("%d", tblData[j][i]);
				} j != c j c {
					System.out.printf("%d ", tblData[j][i]);
				} System.out.printf("%d ", tblData[j][i]) System.out.printf System.out System System.out "%d " tblData[j][i] tblData[j] tblData j i if(j == c) {
					System.out.printf("%d", tblData[j][i]);
				} j == c j c {
					System.out.printf("%d", tblData[j][i]);
				} System.out.printf("%d", tblData[j][i]) System.out.printf System.out System System.out "%d" tblData[j][i] tblData[j] tblData j i 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);

		int r = sc.nextInt();
		int c = sc.nextInt();
		int tblData[][] = new int[c+1][r+1];
		
		//???????????\???
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				tblData[j][i] = sc.nextInt();
			}
		}
		
		//????????????????¨?
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				tblData[c][i] = tblData[c][i] + tblData[j][i];
			}
		}
		
		//????????????????¨?
		for(int j = 0; j < c; j++) {
			for(int i = 0; i < r; i++) {
				tblData[j][r] = tblData[j][r] + tblData[j][i];
			}
		}
		
		//???????¨?
		for(int i = 0; i < r; i++) {
			tblData[c][r] = tblData[c][r] + tblData[c][i];
		}
		
		for(int i = 0; i < r + 1; i++) {
			for(int j = 0; j < c + 1; j++) {
				if(j != c) {
					System.out.printf("%d ", tblData[j][i]);
				} else if(j == c) {
					System.out.printf("%d", tblData[j][i]);
				}
			}
			System.out.println();
		}
	}
} public class Main {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);

		int r = sc.nextInt();
		int c = sc.nextInt();
		int tblData[][] = new int[c+1][r+1];
		
		//???????????\???
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				tblData[j][i] = sc.nextInt();
			}
		}
		
		//????????????????¨?
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				tblData[c][i] = tblData[c][i] + tblData[j][i];
			}
		}
		
		//????????????????¨?
		for(int j = 0; j < c; j++) {
			for(int i = 0; i < r; i++) {
				tblData[j][r] = tblData[j][r] + tblData[j][i];
			}
		}
		
		//???????¨?
		for(int i = 0; i < r; i++) {
			tblData[c][r] = tblData[c][r] + tblData[c][i];
		}
		
		for(int i = 0; i < r + 1; i++) {
			for(int j = 0; j < c + 1; j++) {
				if(j != c) {
					System.out.printf("%d ", tblData[j][i]);
				} else if(j == c) {
					System.out.printf("%d", tblData[j][i]);
				}
			}
			System.out.println();
		}
	}
} Main
import java.util.*;

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

		int r = sc.nextInt();
		int c = sc.nextInt();
		int tblData[][] = new int[c+1][r+1];
		
		//???????????\???
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				tblData[j][i] = sc.nextInt();
			}
		}
		
		//????????????????¨?
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				tblData[c][i] = tblData[c][i] + tblData[j][i];
			}
		}
		
		//????????????????¨?
		for(int j = 0; j < c; j++) {
			for(int i = 0; i < r; i++) {
				tblData[j][r] = tblData[j][r] + tblData[j][i];
			}
		}
		
		//???????¨?
		for(int i = 0; i < r; i++) {
			tblData[c][r] = tblData[c][r] + tblData[c][i];
		}
		
		for(int i = 0; i < r + 1; i++) {
			for(int j = 0; j < c + 1; j++) {
				if(j != c) {
					System.out.printf("%d ", tblData[j][i]);
				} else if(j == c) {
					System.out.printf("%d", tblData[j][i]);
				}
			}
			System.out.println();
		}
	}
}