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

class Main {
	public static void main(String[] args) {
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		try {
			String[] inputList = br.readLine().split(" ");
			int r = Integer.parseInt(inputList[0]);
			int c = Integer.parseInt(inputList[1]);
			int[][] list = new int[r + 1][c + 1];
			int i, j, num, sum;
			for (i = 0; i < r; i++) {
				sum = 0;
				String[] hoge = br.readLine().split(" ");
				for (j = 0; j < c; j++) {
					num = Integer.parseInt(hoge[j]);
					list[i][j] = Integer.parseInt(hoge[j]);
					sum = sum + num;
				}
				list[i][c] = sum;
			}

			for (i = 0; i < c + 1; i++) {
				sum = 0;
				for (j = 0; j < r + 1; j++) {
					sum = sum + list[j][i];
				}
				list[r][i] = sum;
			}

			String output;
			for (i = 0; i < r + 1; i++) {
				output = "";
				for (j = 0; j < c + 1; j++) {
					output = output + list[i][j] + " ";
				}
				System.out.println(output.substring(0, output.length() - 1));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io class Main {
	public static void main(String[] args) {
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		try {
			String[] inputList = br.readLine().split(" ");
			int r = Integer.parseInt(inputList[0]);
			int c = Integer.parseInt(inputList[1]);
			int[][] list = new int[r + 1][c + 1];
			int i, j, num, sum;
			for (i = 0; i < r; i++) {
				sum = 0;
				String[] hoge = br.readLine().split(" ");
				for (j = 0; j < c; j++) {
					num = Integer.parseInt(hoge[j]);
					list[i][j] = Integer.parseInt(hoge[j]);
					sum = sum + num;
				}
				list[i][c] = sum;
			}

			for (i = 0; i < c + 1; i++) {
				sum = 0;
				for (j = 0; j < r + 1; j++) {
					sum = sum + list[j][i];
				}
				list[r][i] = sum;
			}

			String output;
			for (i = 0; i < r + 1; i++) {
				output = "";
				for (j = 0; j < c + 1; j++) {
					output = output + list[i][j] + " ";
				}
				System.out.println(output.substring(0, output.length() - 1));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
} Main public static void main(String[] args) {
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		try {
			String[] inputList = br.readLine().split(" ");
			int r = Integer.parseInt(inputList[0]);
			int c = Integer.parseInt(inputList[1]);
			int[][] list = new int[r + 1][c + 1];
			int i, j, num, sum;
			for (i = 0; i < r; i++) {
				sum = 0;
				String[] hoge = br.readLine().split(" ");
				for (j = 0; j < c; j++) {
					num = Integer.parseInt(hoge[j]);
					list[i][j] = Integer.parseInt(hoge[j]);
					sum = sum + num;
				}
				list[i][c] = sum;
			}

			for (i = 0; i < c + 1; i++) {
				sum = 0;
				for (j = 0; j < r + 1; j++) {
					sum = sum + list[j][i];
				}
				list[r][i] = sum;
			}

			String output;
			for (i = 0; i < r + 1; i++) {
				output = "";
				for (j = 0; j < c + 1; j++) {
					output = output + list[i][j] + " ";
				}
				System.out.println(output.substring(0, output.length() - 1));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	} main {
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		try {
			String[] inputList = br.readLine().split(" ");
			int r = Integer.parseInt(inputList[0]);
			int c = Integer.parseInt(inputList[1]);
			int[][] list = new int[r + 1][c + 1];
			int i, j, num, sum;
			for (i = 0; i < r; i++) {
				sum = 0;
				String[] hoge = br.readLine().split(" ");
				for (j = 0; j < c; j++) {
					num = Integer.parseInt(hoge[j]);
					list[i][j] = Integer.parseInt(hoge[j]);
					sum = sum + num;
				}
				list[i][c] = sum;
			}

			for (i = 0; i < c + 1; i++) {
				sum = 0;
				for (j = 0; j < r + 1; j++) {
					sum = sum + list[j][i];
				}
				list[r][i] = sum;
			}

			String output;
			for (i = 0; i < r + 1; i++) {
				output = "";
				for (j = 0; j < c + 1; j++) {
					output = output + list[i][j] + " ";
				}
				System.out.println(output.substring(0, output.length() - 1));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	} InputStreamReader isr = new InputStreamReader(System.in); isr new InputStreamReader(System.in) BufferedReader br = new BufferedReader(isr); br new BufferedReader(isr) try {
			String[] inputList = br.readLine().split(" ");
			int r = Integer.parseInt(inputList[0]);
			int c = Integer.parseInt(inputList[1]);
			int[][] list = new int[r + 1][c + 1];
			int i, j, num, sum;
			for (i = 0; i < r; i++) {
				sum = 0;
				String[] hoge = br.readLine().split(" ");
				for (j = 0; j < c; j++) {
					num = Integer.parseInt(hoge[j]);
					list[i][j] = Integer.parseInt(hoge[j]);
					sum = sum + num;
				}
				list[i][c] = sum;
			}

			for (i = 0; i < c + 1; i++) {
				sum = 0;
				for (j = 0; j < r + 1; j++) {
					sum = sum + list[j][i];
				}
				list[r][i] = sum;
			}

			String output;
			for (i = 0; i < r + 1; i++) {
				output = "";
				for (j = 0; j < c + 1; j++) {
					output = output + list[i][j] + " ";
				}
				System.out.println(output.substring(0, output.length() - 1));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} IOException e {
			e.printStackTrace();
		} e.printStackTrace() e.printStackTrace e {
			String[] inputList = br.readLine().split(" ");
			int r = Integer.parseInt(inputList[0]);
			int c = Integer.parseInt(inputList[1]);
			int[][] list = new int[r + 1][c + 1];
			int i, j, num, sum;
			for (i = 0; i < r; i++) {
				sum = 0;
				String[] hoge = br.readLine().split(" ");
				for (j = 0; j < c; j++) {
					num = Integer.parseInt(hoge[j]);
					list[i][j] = Integer.parseInt(hoge[j]);
					sum = sum + num;
				}
				list[i][c] = sum;
			}

			for (i = 0; i < c + 1; i++) {
				sum = 0;
				for (j = 0; j < r + 1; j++) {
					sum = sum + list[j][i];
				}
				list[r][i] = sum;
			}

			String output;
			for (i = 0; i < r + 1; i++) {
				output = "";
				for (j = 0; j < c + 1; j++) {
					output = output + list[i][j] + " ";
				}
				System.out.println(output.substring(0, output.length() - 1));
			}
		} String[] inputList = br.readLine().split(" "); inputList br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int r = Integer.parseInt(inputList[0]); r Integer.parseInt(inputList[0]) Integer.parseInt Integer inputList[0] inputList 0 int c = Integer.parseInt(inputList[1]); c Integer.parseInt(inputList[1]) Integer.parseInt Integer inputList[1] inputList 1 int[][] list = new int[r + 1][c + 1]; list new int[r + 1][c + 1] r + 1 r 1 c + 1 c 1 int i i j j num num sum; sum for (i = 0; i < r; i++) {
				sum = 0;
				String[] hoge = br.readLine().split(" ");
				for (j = 0; j < c; j++) {
					num = Integer.parseInt(hoge[j]);
					list[i][j] = Integer.parseInt(hoge[j]);
					sum = sum + num;
				}
				list[i][c] = sum;
			} i = 0; i = 0 i 0 i < r i r i++ i++ i {
				sum = 0;
				String[] hoge = br.readLine().split(" ");
				for (j = 0; j < c; j++) {
					num = Integer.parseInt(hoge[j]);
					list[i][j] = Integer.parseInt(hoge[j]);
					sum = sum + num;
				}
				list[i][c] = sum;
			} {
				sum = 0;
				String[] hoge = br.readLine().split(" ");
				for (j = 0; j < c; j++) {
					num = Integer.parseInt(hoge[j]);
					list[i][j] = Integer.parseInt(hoge[j]);
					sum = sum + num;
				}
				list[i][c] = sum;
			} sum = 0 sum 0 String[] hoge = br.readLine().split(" "); hoge br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " for (j = 0; j < c; j++) {
					num = Integer.parseInt(hoge[j]);
					list[i][j] = Integer.parseInt(hoge[j]);
					sum = sum + num;
				} j = 0; j = 0 j 0 j < c j c j++ j++ j {
					num = Integer.parseInt(hoge[j]);
					list[i][j] = Integer.parseInt(hoge[j]);
					sum = sum + num;
				} {
					num = Integer.parseInt(hoge[j]);
					list[i][j] = Integer.parseInt(hoge[j]);
					sum = sum + num;
				} num = Integer.parseInt(hoge[j]) num Integer.parseInt(hoge[j]) Integer.parseInt Integer hoge[j] hoge j list[i][j] = Integer.parseInt(hoge[j]) list[i][j] list[i] list i j Integer.parseInt(hoge[j]) Integer.parseInt Integer hoge[j] hoge j sum = sum + num sum sum + num sum num list[i][c] = sum list[i][c] list[i] list i c sum for (i = 0; i < c + 1; i++) {
				sum = 0;
				for (j = 0; j < r + 1; j++) {
					sum = sum + list[j][i];
				}
				list[r][i] = sum;
			} i = 0; i = 0 i 0 i < c + 1 i c + 1 c 1 i++ i++ i {
				sum = 0;
				for (j = 0; j < r + 1; j++) {
					sum = sum + list[j][i];
				}
				list[r][i] = sum;
			} {
				sum = 0;
				for (j = 0; j < r + 1; j++) {
					sum = sum + list[j][i];
				}
				list[r][i] = sum;
			} sum = 0 sum 0 for (j = 0; j < r + 1; j++) {
					sum = sum + list[j][i];
				} j = 0; j = 0 j 0 j < r + 1 j r + 1 r 1 j++ j++ j {
					sum = sum + list[j][i];
				} {
					sum = sum + list[j][i];
				} sum = sum + list[j][i] sum sum + list[j][i] sum list[j][i] list[j] list j i list[r][i] = sum list[r][i] list[r] list r i sum String output; output for (i = 0; i < r + 1; i++) {
				output = "";
				for (j = 0; j < c + 1; j++) {
					output = output + list[i][j] + " ";
				}
				System.out.println(output.substring(0, output.length() - 1));
			} i = 0; i = 0 i 0 i < r + 1 i r + 1 r 1 i++ i++ i {
				output = "";
				for (j = 0; j < c + 1; j++) {
					output = output + list[i][j] + " ";
				}
				System.out.println(output.substring(0, output.length() - 1));
			} {
				output = "";
				for (j = 0; j < c + 1; j++) {
					output = output + list[i][j] + " ";
				}
				System.out.println(output.substring(0, output.length() - 1));
			} output = "" output "" for (j = 0; j < c + 1; j++) {
					output = output + list[i][j] + " ";
				} j = 0; j = 0 j 0 j < c + 1 j c + 1 c 1 j++ j++ j {
					output = output + list[i][j] + " ";
				} {
					output = output + list[i][j] + " ";
				} output = output + list[i][j] + " " output output + list[i][j] + " " output + list[i][j] + " " output list[i][j] list[i] list i j " " System.out.println(output.substring(0, output.length() - 1)) System.out.println System.out System System.out output.substring(0, output.length() - 1) output.substring output 0 output.length() - 1 output.length() output.length output 1 String[] args args
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Main {
	public static void main(String[] args) {
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		try {
			String[] inputList = br.readLine().split(" ");
			int r = Integer.parseInt(inputList[0]);
			int c = Integer.parseInt(inputList[1]);
			int[][] list = new int[r + 1][c + 1];
			int i, j, num, sum;
			for (i = 0; i < r; i++) {
				sum = 0;
				String[] hoge = br.readLine().split(" ");
				for (j = 0; j < c; j++) {
					num = Integer.parseInt(hoge[j]);
					list[i][j] = Integer.parseInt(hoge[j]);
					sum = sum + num;
				}
				list[i][c] = sum;
			}

			for (i = 0; i < c + 1; i++) {
				sum = 0;
				for (j = 0; j < r + 1; j++) {
					sum = sum + list[j][i];
				}
				list[r][i] = sum;
			}

			String output;
			for (i = 0; i < r + 1; i++) {
				output = "";
				for (j = 0; j < c + 1; j++) {
					output = output + list[i][j] + " ";
				}
				System.out.println(output.substring(0, output.length() - 1));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}