import java.util.*;

public class Main{
	public static void main(String[] args){

		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] table = new int[r + 1][c + 1];
		int[] sumC = new int[c + 1];

		for(int i = 0; i < r ; i++){
			for(int j = 0; j < c; j++){
				int n = scan.nextInt();
				table[i][j] = n;
			}
		}

		for(int i = 0; i < r + 1; i++){
			int sum = 0;
			for(int j = 0; j < c + 1; j++){
				if(i != r){
					if(j != c){
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} else if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					}
				} else if(i == r){
					table[r][j] = sumC[j];
				}
			}
		}

		for(int i = 0; i < r + 1; i++){
			for(int j = 0; j < c + 1; j++){
				System.out.printf((j != c) ? "%d " : "%d\n", table[i][j]);
			}
		}
	}
} import java.util.*; util.* java public class Main{
	public static void main(String[] args){

		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] table = new int[r + 1][c + 1];
		int[] sumC = new int[c + 1];

		for(int i = 0; i < r ; i++){
			for(int j = 0; j < c; j++){
				int n = scan.nextInt();
				table[i][j] = n;
			}
		}

		for(int i = 0; i < r + 1; i++){
			int sum = 0;
			for(int j = 0; j < c + 1; j++){
				if(i != r){
					if(j != c){
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} else if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					}
				} else if(i == r){
					table[r][j] = sumC[j];
				}
			}
		}

		for(int i = 0; i < r + 1; i++){
			for(int j = 0; j < c + 1; j++){
				System.out.printf((j != c) ? "%d " : "%d\n", table[i][j]);
			}
		}
	}
} Main public static void main(String[] args){

		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] table = new int[r + 1][c + 1];
		int[] sumC = new int[c + 1];

		for(int i = 0; i < r ; i++){
			for(int j = 0; j < c; j++){
				int n = scan.nextInt();
				table[i][j] = n;
			}
		}

		for(int i = 0; i < r + 1; i++){
			int sum = 0;
			for(int j = 0; j < c + 1; j++){
				if(i != r){
					if(j != c){
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} else if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					}
				} else if(i == r){
					table[r][j] = sumC[j];
				}
			}
		}

		for(int i = 0; i < r + 1; i++){
			for(int j = 0; j < c + 1; j++){
				System.out.printf((j != c) ? "%d " : "%d\n", table[i][j]);
			}
		}
	} main {

		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] table = new int[r + 1][c + 1];
		int[] sumC = new int[c + 1];

		for(int i = 0; i < r ; i++){
			for(int j = 0; j < c; j++){
				int n = scan.nextInt();
				table[i][j] = n;
			}
		}

		for(int i = 0; i < r + 1; i++){
			int sum = 0;
			for(int j = 0; j < c + 1; j++){
				if(i != r){
					if(j != c){
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} else if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					}
				} else if(i == r){
					table[r][j] = sumC[j];
				}
			}
		}

		for(int i = 0; i < r + 1; i++){
			for(int j = 0; j < c + 1; j++){
				System.out.printf((j != c) ? "%d " : "%d\n", table[i][j]);
			}
		}
	} 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[][] table = new int[r + 1][c + 1]; table new int[r + 1][c + 1] r + 1 r 1 c + 1 c 1 int[] sumC = new int[c + 1]; sumC new int[c + 1] c + 1 c 1 for(int i = 0; i < r ; i++){
			for(int j = 0; j < c; j++){
				int n = scan.nextInt();
				table[i][j] = n;
			}
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			for(int j = 0; j < c; j++){
				int n = scan.nextInt();
				table[i][j] = n;
			}
		} {
			for(int j = 0; j < c; j++){
				int n = scan.nextInt();
				table[i][j] = n;
			}
		} for(int j = 0; j < c; j++){
				int n = scan.nextInt();
				table[i][j] = n;
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				int n = scan.nextInt();
				table[i][j] = n;
			} {
				int n = scan.nextInt();
				table[i][j] = n;
			} int n = scan.nextInt(); n scan.nextInt() scan.nextInt scan table[i][j] = n table[i][j] table[i] table i j n for(int i = 0; i < r + 1; i++){
			int sum = 0;
			for(int j = 0; j < c + 1; j++){
				if(i != r){
					if(j != c){
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} else if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					}
				} else if(i == r){
					table[r][j] = sumC[j];
				}
			}
		} int i = 0; int i = 0; i 0 i < r + 1 i r + 1 r 1 i++ i++ i {
			int sum = 0;
			for(int j = 0; j < c + 1; j++){
				if(i != r){
					if(j != c){
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} else if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					}
				} else if(i == r){
					table[r][j] = sumC[j];
				}
			}
		} {
			int sum = 0;
			for(int j = 0; j < c + 1; j++){
				if(i != r){
					if(j != c){
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} else if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					}
				} else if(i == r){
					table[r][j] = sumC[j];
				}
			}
		} int sum = 0; sum 0 for(int j = 0; j < c + 1; j++){
				if(i != r){
					if(j != c){
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} else if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					}
				} else if(i == r){
					table[r][j] = sumC[j];
				}
			} int j = 0; int j = 0; j 0 j < c + 1 j c + 1 c 1 j++ j++ j {
				if(i != r){
					if(j != c){
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} else if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					}
				} else if(i == r){
					table[r][j] = sumC[j];
				}
			} {
				if(i != r){
					if(j != c){
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} else if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					}
				} else if(i == r){
					table[r][j] = sumC[j];
				}
			} if(i != r){
					if(j != c){
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} else if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					}
				} else if(i == r){
					table[r][j] = sumC[j];
				} i != r i r {
					if(j != c){
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} else if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					}
				} if(j != c){
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} else if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					} j != c j c {
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} sum = sum + table[i][j] sum sum + table[i][j] sum table[i][j] table[i] table i j sumC[j] = sumC[j] + table[i][j] sumC[j] sumC j sumC[j] + table[i][j] sumC[j] sumC j table[i][j] table[i] table i j if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					} j == c j c {
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					} table[i][c] = sum table[i][c] table[i] table i c sum sumC[c] = sumC[c] + table[i][c] sumC[c] sumC c sumC[c] + table[i][c] sumC[c] sumC c table[i][c] table[i] table i c if(i == r){
					table[r][j] = sumC[j];
				} i == r i r {
					table[r][j] = sumC[j];
				} table[r][j] = sumC[j] table[r][j] table[r] table r j sumC[j] sumC j for(int i = 0; i < r + 1; i++){
			for(int j = 0; j < c + 1; j++){
				System.out.printf((j != c) ? "%d " : "%d\n", table[i][j]);
			}
		} 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++){
				System.out.printf((j != c) ? "%d " : "%d\n", table[i][j]);
			}
		} {
			for(int j = 0; j < c + 1; j++){
				System.out.printf((j != c) ? "%d " : "%d\n", table[i][j]);
			}
		} for(int j = 0; j < c + 1; j++){
				System.out.printf((j != c) ? "%d " : "%d\n", table[i][j]);
			} int j = 0; int j = 0; j 0 j < c + 1 j c + 1 c 1 j++ j++ j {
				System.out.printf((j != c) ? "%d " : "%d\n", table[i][j]);
			} {
				System.out.printf((j != c) ? "%d " : "%d\n", table[i][j]);
			} System.out.printf((j != c) ? "%d " : "%d\n", table[i][j]) System.out.printf System.out System System.out (j != c) ? "%d " : "%d\n" (j != c) j c "%d " "%d\n" table[i][j] table[i] table i j String[] args args public class Main{
	public static void main(String[] args){

		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] table = new int[r + 1][c + 1];
		int[] sumC = new int[c + 1];

		for(int i = 0; i < r ; i++){
			for(int j = 0; j < c; j++){
				int n = scan.nextInt();
				table[i][j] = n;
			}
		}

		for(int i = 0; i < r + 1; i++){
			int sum = 0;
			for(int j = 0; j < c + 1; j++){
				if(i != r){
					if(j != c){
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} else if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					}
				} else if(i == r){
					table[r][j] = sumC[j];
				}
			}
		}

		for(int i = 0; i < r + 1; i++){
			for(int j = 0; j < c + 1; j++){
				System.out.printf((j != c) ? "%d " : "%d\n", table[i][j]);
			}
		}
	}
} public class Main{
	public static void main(String[] args){

		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] table = new int[r + 1][c + 1];
		int[] sumC = new int[c + 1];

		for(int i = 0; i < r ; i++){
			for(int j = 0; j < c; j++){
				int n = scan.nextInt();
				table[i][j] = n;
			}
		}

		for(int i = 0; i < r + 1; i++){
			int sum = 0;
			for(int j = 0; j < c + 1; j++){
				if(i != r){
					if(j != c){
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} else if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					}
				} else if(i == r){
					table[r][j] = sumC[j];
				}
			}
		}

		for(int i = 0; i < r + 1; i++){
			for(int j = 0; j < c + 1; j++){
				System.out.printf((j != c) ? "%d " : "%d\n", table[i][j]);
			}
		}
	}
} Main
import java.util.*;

public class Main{
	public static void main(String[] args){

		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] table = new int[r + 1][c + 1];
		int[] sumC = new int[c + 1];

		for(int i = 0; i < r ; i++){
			for(int j = 0; j < c; j++){
				int n = scan.nextInt();
				table[i][j] = n;
			}
		}

		for(int i = 0; i < r + 1; i++){
			int sum = 0;
			for(int j = 0; j < c + 1; j++){
				if(i != r){
					if(j != c){
						sum = sum + table[i][j];
						sumC[j] = sumC[j] + table[i][j];
					} else if(j == c){
						table[i][c] = sum;
						sumC[c] = sumC[c] + table[i][c];
					}
				} else if(i == r){
					table[r][j] = sumC[j];
				}
			}
		}

		for(int i = 0; i < r + 1; i++){
			for(int j = 0; j < c + 1; j++){
				System.out.printf((j != c) ? "%d " : "%d\n", table[i][j]);
			}
		}
	}
}