import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int r, c;
		r = in.nextInt();
		c = in.nextInt();
		int table[][] = new int[r+1][c+1];
		for(int i=0;i<r;i++) {
			for(int j=0;j<c;j++) {
				table[i][j] = in.nextInt();
			}
		}
		int[] colSum = new int[c+1];
		for(int i=0;i<r;i++) {
			int rowSum = 0;
			for(int j=0;j<=c;j++) {
				rowSum += table[i][j];
				if(j == c) {
					table[i][j] = rowSum;
				}
				colSum[j] += table[i][j];
			}
		}
		for(int i=0;i<=c;i++) {
			table[r][i] = colSum[i];
		}

		for(int i=0;i<=r;i++) {
			for(int j=0;j<=c;j++) {
				System.out.print(String.format("%d%s", table[i][j], (j == c) ? "": " "));
			}
			System.out.println("");
		}
	}
} import java.util.ArrayList; ArrayList java.util import java.util.List; List java.util import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int r, c;
		r = in.nextInt();
		c = in.nextInt();
		int table[][] = new int[r+1][c+1];
		for(int i=0;i<r;i++) {
			for(int j=0;j<c;j++) {
				table[i][j] = in.nextInt();
			}
		}
		int[] colSum = new int[c+1];
		for(int i=0;i<r;i++) {
			int rowSum = 0;
			for(int j=0;j<=c;j++) {
				rowSum += table[i][j];
				if(j == c) {
					table[i][j] = rowSum;
				}
				colSum[j] += table[i][j];
			}
		}
		for(int i=0;i<=c;i++) {
			table[r][i] = colSum[i];
		}

		for(int i=0;i<=r;i++) {
			for(int j=0;j<=c;j++) {
				System.out.print(String.format("%d%s", table[i][j], (j == c) ? "": " "));
			}
			System.out.println("");
		}
	}
} Main public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int r, c;
		r = in.nextInt();
		c = in.nextInt();
		int table[][] = new int[r+1][c+1];
		for(int i=0;i<r;i++) {
			for(int j=0;j<c;j++) {
				table[i][j] = in.nextInt();
			}
		}
		int[] colSum = new int[c+1];
		for(int i=0;i<r;i++) {
			int rowSum = 0;
			for(int j=0;j<=c;j++) {
				rowSum += table[i][j];
				if(j == c) {
					table[i][j] = rowSum;
				}
				colSum[j] += table[i][j];
			}
		}
		for(int i=0;i<=c;i++) {
			table[r][i] = colSum[i];
		}

		for(int i=0;i<=r;i++) {
			for(int j=0;j<=c;j++) {
				System.out.print(String.format("%d%s", table[i][j], (j == c) ? "": " "));
			}
			System.out.println("");
		}
	} main {
		Scanner in = new Scanner(System.in);
		int r, c;
		r = in.nextInt();
		c = in.nextInt();
		int table[][] = new int[r+1][c+1];
		for(int i=0;i<r;i++) {
			for(int j=0;j<c;j++) {
				table[i][j] = in.nextInt();
			}
		}
		int[] colSum = new int[c+1];
		for(int i=0;i<r;i++) {
			int rowSum = 0;
			for(int j=0;j<=c;j++) {
				rowSum += table[i][j];
				if(j == c) {
					table[i][j] = rowSum;
				}
				colSum[j] += table[i][j];
			}
		}
		for(int i=0;i<=c;i++) {
			table[r][i] = colSum[i];
		}

		for(int i=0;i<=r;i++) {
			for(int j=0;j<=c;j++) {
				System.out.print(String.format("%d%s", table[i][j], (j == c) ? "": " "));
			}
			System.out.println("");
		}
	} Scanner in = new Scanner(System.in); in new Scanner(System.in) int r r c; c r = in.nextInt() r in.nextInt() in.nextInt in c = in.nextInt() c in.nextInt() in.nextInt in int table[][] = new int[r+1][c+1]; table 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++) {
				table[i][j] = in.nextInt();
			}
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			for(int j=0;j<c;j++) {
				table[i][j] = in.nextInt();
			}
		} {
			for(int j=0;j<c;j++) {
				table[i][j] = in.nextInt();
			}
		} for(int j=0;j<c;j++) {
				table[i][j] = in.nextInt();
			} int j=0; int j=0; j 0 j<c j c j++ j++ j {
				table[i][j] = in.nextInt();
			} {
				table[i][j] = in.nextInt();
			} table[i][j] = in.nextInt() table[i][j] table[i] table i j in.nextInt() in.nextInt in int[] colSum = new int[c+1]; colSum new int[c+1] c+1 c 1 for(int i=0;i<r;i++) {
			int rowSum = 0;
			for(int j=0;j<=c;j++) {
				rowSum += table[i][j];
				if(j == c) {
					table[i][j] = rowSum;
				}
				colSum[j] += table[i][j];
			}
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			int rowSum = 0;
			for(int j=0;j<=c;j++) {
				rowSum += table[i][j];
				if(j == c) {
					table[i][j] = rowSum;
				}
				colSum[j] += table[i][j];
			}
		} {
			int rowSum = 0;
			for(int j=0;j<=c;j++) {
				rowSum += table[i][j];
				if(j == c) {
					table[i][j] = rowSum;
				}
				colSum[j] += table[i][j];
			}
		} int rowSum = 0; rowSum 0 for(int j=0;j<=c;j++) {
				rowSum += table[i][j];
				if(j == c) {
					table[i][j] = rowSum;
				}
				colSum[j] += table[i][j];
			} int j=0; int j=0; j 0 j<=c j c j++ j++ j {
				rowSum += table[i][j];
				if(j == c) {
					table[i][j] = rowSum;
				}
				colSum[j] += table[i][j];
			} {
				rowSum += table[i][j];
				if(j == c) {
					table[i][j] = rowSum;
				}
				colSum[j] += table[i][j];
			} rowSum += table[i][j] rowSum table[i][j] table[i] table i j if(j == c) {
					table[i][j] = rowSum;
				} j == c j c {
					table[i][j] = rowSum;
				} table[i][j] = rowSum table[i][j] table[i] table i j rowSum colSum[j] += table[i][j] colSum[j] colSum j table[i][j] table[i] table i j for(int i=0;i<=c;i++) {
			table[r][i] = colSum[i];
		} int i=0; int i=0; i 0 i<=c i c i++ i++ i {
			table[r][i] = colSum[i];
		} {
			table[r][i] = colSum[i];
		} table[r][i] = colSum[i] table[r][i] table[r] table r i colSum[i] colSum i for(int i=0;i<=r;i++) {
			for(int j=0;j<=c;j++) {
				System.out.print(String.format("%d%s", table[i][j], (j == c) ? "": " "));
			}
			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++) {
				System.out.print(String.format("%d%s", table[i][j], (j == c) ? "": " "));
			}
			System.out.println("");
		} {
			for(int j=0;j<=c;j++) {
				System.out.print(String.format("%d%s", table[i][j], (j == c) ? "": " "));
			}
			System.out.println("");
		} for(int j=0;j<=c;j++) {
				System.out.print(String.format("%d%s", table[i][j], (j == c) ? "": " "));
			} int j=0; int j=0; j 0 j<=c j c j++ j++ j {
				System.out.print(String.format("%d%s", table[i][j], (j == c) ? "": " "));
			} {
				System.out.print(String.format("%d%s", table[i][j], (j == c) ? "": " "));
			} System.out.print(String.format("%d%s", table[i][j], (j == c) ? "": " ")) System.out.print System.out System System.out String.format("%d%s", table[i][j], (j == c) ? "": " ") String.format String "%d%s" table[i][j] table[i] table i j (j == c) ? "": " " (j == c) j c "" " " System.out.println("") System.out.println System.out System System.out "" String[] args args public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int r, c;
		r = in.nextInt();
		c = in.nextInt();
		int table[][] = new int[r+1][c+1];
		for(int i=0;i<r;i++) {
			for(int j=0;j<c;j++) {
				table[i][j] = in.nextInt();
			}
		}
		int[] colSum = new int[c+1];
		for(int i=0;i<r;i++) {
			int rowSum = 0;
			for(int j=0;j<=c;j++) {
				rowSum += table[i][j];
				if(j == c) {
					table[i][j] = rowSum;
				}
				colSum[j] += table[i][j];
			}
		}
		for(int i=0;i<=c;i++) {
			table[r][i] = colSum[i];
		}

		for(int i=0;i<=r;i++) {
			for(int j=0;j<=c;j++) {
				System.out.print(String.format("%d%s", table[i][j], (j == c) ? "": " "));
			}
			System.out.println("");
		}
	}
} public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int r, c;
		r = in.nextInt();
		c = in.nextInt();
		int table[][] = new int[r+1][c+1];
		for(int i=0;i<r;i++) {
			for(int j=0;j<c;j++) {
				table[i][j] = in.nextInt();
			}
		}
		int[] colSum = new int[c+1];
		for(int i=0;i<r;i++) {
			int rowSum = 0;
			for(int j=0;j<=c;j++) {
				rowSum += table[i][j];
				if(j == c) {
					table[i][j] = rowSum;
				}
				colSum[j] += table[i][j];
			}
		}
		for(int i=0;i<=c;i++) {
			table[r][i] = colSum[i];
		}

		for(int i=0;i<=r;i++) {
			for(int j=0;j<=c;j++) {
				System.out.print(String.format("%d%s", table[i][j], (j == c) ? "": " "));
			}
			System.out.println("");
		}
	}
} Main
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int r, c;
		r = in.nextInt();
		c = in.nextInt();
		int table[][] = new int[r+1][c+1];
		for(int i=0;i<r;i++) {
			for(int j=0;j<c;j++) {
				table[i][j] = in.nextInt();
			}
		}
		int[] colSum = new int[c+1];
		for(int i=0;i<r;i++) {
			int rowSum = 0;
			for(int j=0;j<=c;j++) {
				rowSum += table[i][j];
				if(j == c) {
					table[i][j] = rowSum;
				}
				colSum[j] += table[i][j];
			}
		}
		for(int i=0;i<=c;i++) {
			table[r][i] = colSum[i];
		}

		for(int i=0;i<=r;i++) {
			for(int j=0;j<=c;j++) {
				System.out.print(String.format("%d%s", table[i][j], (j == c) ? "": " "));
			}
			System.out.println("");
		}
	}
}