
import java.util.*;

public class Main {
	public static void main(String args[]) {
		Scanner scan = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int r, c, sum,sum2;
		r = scan.nextInt();
		c = scan.nextInt();
		sum = 0;// ????¨????
		sum2 = 0;//??¨??????????¨????
		// ????¬???????????????????
		int num[][] = new int[r + 1][c + 1];
		// ?????????????´?
		for (int count = 0; count < r; count++) {
			for (int count2 = 0; count2 < c; count2++) {
				num[count][count2] = scan.nextInt();
			}
		}
		
		// ?¨??????¨StringBuilder???????´??????????????????? r=1 c=8
		for (int count3 = 0; count3 < (r + 1); count3++) {
			for (int count4 = 0; count4 < (c + 1); count4++) {
				if(count3 == r){break;}//????????????????????????????????????
				if (count4 == c) {// ???????¨??????¨???
					// ????????¨??????????¨?
					for (int count5 = 0; count5 < (c+1); count5++) {
						sum = sum + num[count3][count5];
					}
					sb.append(sum);
					sb.append("\n");
					sum = 0;// sum????????????
				} else {// ??????????????¨???
					sb.append(num[count3][count4]);
					sb.append(" ");
				}
			}
			sum = 0;//sum ????????????
			if(count3 == r){//???????¨??????¨???
				for(int count6=0;count6<c;count6++){
					for(int count7=0;count7<r;count7++){
						sum = sum + num[count7][count6];
					}
					sum2 = sum2 + sum;
					sb.append(sum);
					sb.append(" ");
					sum = 0;//sum????????????
				}
			}
		}
		sb.append(sum2);
		System.out.println(new String(sb));

	}
} import java.util.*; util.* java public class Main {
	public static void main(String args[]) {
		Scanner scan = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int r, c, sum,sum2;
		r = scan.nextInt();
		c = scan.nextInt();
		sum = 0;// ????¨????
		sum2 = 0;//??¨??????????¨????
		// ????¬???????????????????
		int num[][] = new int[r + 1][c + 1];
		// ?????????????´?
		for (int count = 0; count < r; count++) {
			for (int count2 = 0; count2 < c; count2++) {
				num[count][count2] = scan.nextInt();
			}
		}
		
		// ?¨??????¨StringBuilder???????´??????????????????? r=1 c=8
		for (int count3 = 0; count3 < (r + 1); count3++) {
			for (int count4 = 0; count4 < (c + 1); count4++) {
				if(count3 == r){break;}//????????????????????????????????????
				if (count4 == c) {// ???????¨??????¨???
					// ????????¨??????????¨?
					for (int count5 = 0; count5 < (c+1); count5++) {
						sum = sum + num[count3][count5];
					}
					sb.append(sum);
					sb.append("\n");
					sum = 0;// sum????????????
				} else {// ??????????????¨???
					sb.append(num[count3][count4]);
					sb.append(" ");
				}
			}
			sum = 0;//sum ????????????
			if(count3 == r){//???????¨??????¨???
				for(int count6=0;count6<c;count6++){
					for(int count7=0;count7<r;count7++){
						sum = sum + num[count7][count6];
					}
					sum2 = sum2 + sum;
					sb.append(sum);
					sb.append(" ");
					sum = 0;//sum????????????
				}
			}
		}
		sb.append(sum2);
		System.out.println(new String(sb));

	}
} Main public static void main(String args[]) {
		Scanner scan = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int r, c, sum,sum2;
		r = scan.nextInt();
		c = scan.nextInt();
		sum = 0;// ????¨????
		sum2 = 0;//??¨??????????¨????
		// ????¬???????????????????
		int num[][] = new int[r + 1][c + 1];
		// ?????????????´?
		for (int count = 0; count < r; count++) {
			for (int count2 = 0; count2 < c; count2++) {
				num[count][count2] = scan.nextInt();
			}
		}
		
		// ?¨??????¨StringBuilder???????´??????????????????? r=1 c=8
		for (int count3 = 0; count3 < (r + 1); count3++) {
			for (int count4 = 0; count4 < (c + 1); count4++) {
				if(count3 == r){break;}//????????????????????????????????????
				if (count4 == c) {// ???????¨??????¨???
					// ????????¨??????????¨?
					for (int count5 = 0; count5 < (c+1); count5++) {
						sum = sum + num[count3][count5];
					}
					sb.append(sum);
					sb.append("\n");
					sum = 0;// sum????????????
				} else {// ??????????????¨???
					sb.append(num[count3][count4]);
					sb.append(" ");
				}
			}
			sum = 0;//sum ????????????
			if(count3 == r){//???????¨??????¨???
				for(int count6=0;count6<c;count6++){
					for(int count7=0;count7<r;count7++){
						sum = sum + num[count7][count6];
					}
					sum2 = sum2 + sum;
					sb.append(sum);
					sb.append(" ");
					sum = 0;//sum????????????
				}
			}
		}
		sb.append(sum2);
		System.out.println(new String(sb));

	} main {
		Scanner scan = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int r, c, sum,sum2;
		r = scan.nextInt();
		c = scan.nextInt();
		sum = 0;// ????¨????
		sum2 = 0;//??¨??????????¨????
		// ????¬???????????????????
		int num[][] = new int[r + 1][c + 1];
		// ?????????????´?
		for (int count = 0; count < r; count++) {
			for (int count2 = 0; count2 < c; count2++) {
				num[count][count2] = scan.nextInt();
			}
		}
		
		// ?¨??????¨StringBuilder???????´??????????????????? r=1 c=8
		for (int count3 = 0; count3 < (r + 1); count3++) {
			for (int count4 = 0; count4 < (c + 1); count4++) {
				if(count3 == r){break;}//????????????????????????????????????
				if (count4 == c) {// ???????¨??????¨???
					// ????????¨??????????¨?
					for (int count5 = 0; count5 < (c+1); count5++) {
						sum = sum + num[count3][count5];
					}
					sb.append(sum);
					sb.append("\n");
					sum = 0;// sum????????????
				} else {// ??????????????¨???
					sb.append(num[count3][count4]);
					sb.append(" ");
				}
			}
			sum = 0;//sum ????????????
			if(count3 == r){//???????¨??????¨???
				for(int count6=0;count6<c;count6++){
					for(int count7=0;count7<r;count7++){
						sum = sum + num[count7][count6];
					}
					sum2 = sum2 + sum;
					sb.append(sum);
					sb.append(" ");
					sum = 0;//sum????????????
				}
			}
		}
		sb.append(sum2);
		System.out.println(new String(sb));

	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) StringBuilder sb = new StringBuilder(); sb new StringBuilder() int r r c c sum sum sum2; sum2 r = scan.nextInt() r scan.nextInt() scan.nextInt scan c = scan.nextInt() c scan.nextInt() scan.nextInt scan sum = 0 sum 0 sum2 = 0 sum2 0 int num[][] = new int[r + 1][c + 1]; num new int[r + 1][c + 1] r + 1 r 1 c + 1 c 1 for (int count = 0; count < r; count++) {
			for (int count2 = 0; count2 < c; count2++) {
				num[count][count2] = scan.nextInt();
			}
		} int count = 0; int count = 0; count 0 count < r count r count++ count++ count {
			for (int count2 = 0; count2 < c; count2++) {
				num[count][count2] = scan.nextInt();
			}
		} {
			for (int count2 = 0; count2 < c; count2++) {
				num[count][count2] = scan.nextInt();
			}
		} for (int count2 = 0; count2 < c; count2++) {
				num[count][count2] = scan.nextInt();
			} int count2 = 0; int count2 = 0; count2 0 count2 < c count2 c count2++ count2++ count2 {
				num[count][count2] = scan.nextInt();
			} {
				num[count][count2] = scan.nextInt();
			} num[count][count2] = scan.nextInt() num[count][count2] num[count] num count count2 scan.nextInt() scan.nextInt scan for (int count3 = 0; count3 < (r + 1); count3++) {
			for (int count4 = 0; count4 < (c + 1); count4++) {
				if(count3 == r){break;}//????????????????????????????????????
				if (count4 == c) {// ???????¨??????¨???
					// ????????¨??????????¨?
					for (int count5 = 0; count5 < (c+1); count5++) {
						sum = sum + num[count3][count5];
					}
					sb.append(sum);
					sb.append("\n");
					sum = 0;// sum????????????
				} else {// ??????????????¨???
					sb.append(num[count3][count4]);
					sb.append(" ");
				}
			}
			sum = 0;//sum ????????????
			if(count3 == r){//???????¨??????¨???
				for(int count6=0;count6<c;count6++){
					for(int count7=0;count7<r;count7++){
						sum = sum + num[count7][count6];
					}
					sum2 = sum2 + sum;
					sb.append(sum);
					sb.append(" ");
					sum = 0;//sum????????????
				}
			}
		} int count3 = 0; int count3 = 0; count3 0 count3 < (r + 1) count3 (r + 1) r 1 count3++ count3++ count3 {
			for (int count4 = 0; count4 < (c + 1); count4++) {
				if(count3 == r){break;}//????????????????????????????????????
				if (count4 == c) {// ???????¨??????¨???
					// ????????¨??????????¨?
					for (int count5 = 0; count5 < (c+1); count5++) {
						sum = sum + num[count3][count5];
					}
					sb.append(sum);
					sb.append("\n");
					sum = 0;// sum????????????
				} else {// ??????????????¨???
					sb.append(num[count3][count4]);
					sb.append(" ");
				}
			}
			sum = 0;//sum ????????????
			if(count3 == r){//???????¨??????¨???
				for(int count6=0;count6<c;count6++){
					for(int count7=0;count7<r;count7++){
						sum = sum + num[count7][count6];
					}
					sum2 = sum2 + sum;
					sb.append(sum);
					sb.append(" ");
					sum = 0;//sum????????????
				}
			}
		} {
			for (int count4 = 0; count4 < (c + 1); count4++) {
				if(count3 == r){break;}//????????????????????????????????????
				if (count4 == c) {// ???????¨??????¨???
					// ????????¨??????????¨?
					for (int count5 = 0; count5 < (c+1); count5++) {
						sum = sum + num[count3][count5];
					}
					sb.append(sum);
					sb.append("\n");
					sum = 0;// sum????????????
				} else {// ??????????????¨???
					sb.append(num[count3][count4]);
					sb.append(" ");
				}
			}
			sum = 0;//sum ????????????
			if(count3 == r){//???????¨??????¨???
				for(int count6=0;count6<c;count6++){
					for(int count7=0;count7<r;count7++){
						sum = sum + num[count7][count6];
					}
					sum2 = sum2 + sum;
					sb.append(sum);
					sb.append(" ");
					sum = 0;//sum????????????
				}
			}
		} for (int count4 = 0; count4 < (c + 1); count4++) {
				if(count3 == r){break;}//????????????????????????????????????
				if (count4 == c) {// ???????¨??????¨???
					// ????????¨??????????¨?
					for (int count5 = 0; count5 < (c+1); count5++) {
						sum = sum + num[count3][count5];
					}
					sb.append(sum);
					sb.append("\n");
					sum = 0;// sum????????????
				} else {// ??????????????¨???
					sb.append(num[count3][count4]);
					sb.append(" ");
				}
			} int count4 = 0; int count4 = 0; count4 0 count4 < (c + 1) count4 (c + 1) c 1 count4++ count4++ count4 {
				if(count3 == r){break;}//????????????????????????????????????
				if (count4 == c) {// ???????¨??????¨???
					// ????????¨??????????¨?
					for (int count5 = 0; count5 < (c+1); count5++) {
						sum = sum + num[count3][count5];
					}
					sb.append(sum);
					sb.append("\n");
					sum = 0;// sum????????????
				} else {// ??????????????¨???
					sb.append(num[count3][count4]);
					sb.append(" ");
				}
			} {
				if(count3 == r){break;}//????????????????????????????????????
				if (count4 == c) {// ???????¨??????¨???
					// ????????¨??????????¨?
					for (int count5 = 0; count5 < (c+1); count5++) {
						sum = sum + num[count3][count5];
					}
					sb.append(sum);
					sb.append("\n");
					sum = 0;// sum????????????
				} else {// ??????????????¨???
					sb.append(num[count3][count4]);
					sb.append(" ");
				}
			} if(count3 == r){break;}//???????????????????????????????????? count3 == r count3 r {break;} break; if (count4 == c) {// ???????¨??????¨???
					// ????????¨??????????¨?
					for (int count5 = 0; count5 < (c+1); count5++) {
						sum = sum + num[count3][count5];
					}
					sb.append(sum);
					sb.append("\n");
					sum = 0;// sum????????????
				} else {// ??????????????¨???
					sb.append(num[count3][count4]);
					sb.append(" ");
				} count4 == c count4 c {// ???????¨??????¨???
					// ????????¨??????????¨?
					for (int count5 = 0; count5 < (c+1); count5++) {
						sum = sum + num[count3][count5];
					}
					sb.append(sum);
					sb.append("\n");
					sum = 0;// sum????????????
				} for (int count5 = 0; count5 < (c+1); count5++) {
						sum = sum + num[count3][count5];
					} int count5 = 0; int count5 = 0; count5 0 count5 < (c+1) count5 (c+1) c 1 count5++ count5++ count5 {
						sum = sum + num[count3][count5];
					} {
						sum = sum + num[count3][count5];
					} sum = sum + num[count3][count5] sum sum + num[count3][count5] sum num[count3][count5] num[count3] num count3 count5 sb.append(sum) sb.append sb sum sb.append("\n") sb.append sb "\n" sum = 0 sum 0 {// ??????????????¨???
					sb.append(num[count3][count4]);
					sb.append(" ");
				} sb.append(num[count3][count4]) sb.append sb num[count3][count4] num[count3] num count3 count4 sb.append(" ") sb.append sb " " sum = 0 sum 0 if(count3 == r){//???????¨??????¨???
				for(int count6=0;count6<c;count6++){
					for(int count7=0;count7<r;count7++){
						sum = sum + num[count7][count6];
					}
					sum2 = sum2 + sum;
					sb.append(sum);
					sb.append(" ");
					sum = 0;//sum????????????
				}
			} count3 == r count3 r {//???????¨??????¨???
				for(int count6=0;count6<c;count6++){
					for(int count7=0;count7<r;count7++){
						sum = sum + num[count7][count6];
					}
					sum2 = sum2 + sum;
					sb.append(sum);
					sb.append(" ");
					sum = 0;//sum????????????
				}
			} for(int count6=0;count6<c;count6++){
					for(int count7=0;count7<r;count7++){
						sum = sum + num[count7][count6];
					}
					sum2 = sum2 + sum;
					sb.append(sum);
					sb.append(" ");
					sum = 0;//sum????????????
				} int count6=0; int count6=0; count6 0 count6<c count6 c count6++ count6++ count6 {
					for(int count7=0;count7<r;count7++){
						sum = sum + num[count7][count6];
					}
					sum2 = sum2 + sum;
					sb.append(sum);
					sb.append(" ");
					sum = 0;//sum????????????
				} {
					for(int count7=0;count7<r;count7++){
						sum = sum + num[count7][count6];
					}
					sum2 = sum2 + sum;
					sb.append(sum);
					sb.append(" ");
					sum = 0;//sum????????????
				} for(int count7=0;count7<r;count7++){
						sum = sum + num[count7][count6];
					} int count7=0; int count7=0; count7 0 count7<r count7 r count7++ count7++ count7 {
						sum = sum + num[count7][count6];
					} {
						sum = sum + num[count7][count6];
					} sum = sum + num[count7][count6] sum sum + num[count7][count6] sum num[count7][count6] num[count7] num count7 count6 sum2 = sum2 + sum sum2 sum2 + sum sum2 sum sb.append(sum) sb.append sb sum sb.append(" ") sb.append sb " " sum = 0 sum 0 sb.append(sum2) sb.append sb sum2 System.out.println(new String(sb)) System.out.println System.out System System.out new String(sb) String args[] args public class Main {
	public static void main(String args[]) {
		Scanner scan = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int r, c, sum,sum2;
		r = scan.nextInt();
		c = scan.nextInt();
		sum = 0;// ????¨????
		sum2 = 0;//??¨??????????¨????
		// ????¬???????????????????
		int num[][] = new int[r + 1][c + 1];
		// ?????????????´?
		for (int count = 0; count < r; count++) {
			for (int count2 = 0; count2 < c; count2++) {
				num[count][count2] = scan.nextInt();
			}
		}
		
		// ?¨??????¨StringBuilder???????´??????????????????? r=1 c=8
		for (int count3 = 0; count3 < (r + 1); count3++) {
			for (int count4 = 0; count4 < (c + 1); count4++) {
				if(count3 == r){break;}//????????????????????????????????????
				if (count4 == c) {// ???????¨??????¨???
					// ????????¨??????????¨?
					for (int count5 = 0; count5 < (c+1); count5++) {
						sum = sum + num[count3][count5];
					}
					sb.append(sum);
					sb.append("\n");
					sum = 0;// sum????????????
				} else {// ??????????????¨???
					sb.append(num[count3][count4]);
					sb.append(" ");
				}
			}
			sum = 0;//sum ????????????
			if(count3 == r){//???????¨??????¨???
				for(int count6=0;count6<c;count6++){
					for(int count7=0;count7<r;count7++){
						sum = sum + num[count7][count6];
					}
					sum2 = sum2 + sum;
					sb.append(sum);
					sb.append(" ");
					sum = 0;//sum????????????
				}
			}
		}
		sb.append(sum2);
		System.out.println(new String(sb));

	}
} public class Main {
	public static void main(String args[]) {
		Scanner scan = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int r, c, sum,sum2;
		r = scan.nextInt();
		c = scan.nextInt();
		sum = 0;// ????¨????
		sum2 = 0;//??¨??????????¨????
		// ????¬???????????????????
		int num[][] = new int[r + 1][c + 1];
		// ?????????????´?
		for (int count = 0; count < r; count++) {
			for (int count2 = 0; count2 < c; count2++) {
				num[count][count2] = scan.nextInt();
			}
		}
		
		// ?¨??????¨StringBuilder???????´??????????????????? r=1 c=8
		for (int count3 = 0; count3 < (r + 1); count3++) {
			for (int count4 = 0; count4 < (c + 1); count4++) {
				if(count3 == r){break;}//????????????????????????????????????
				if (count4 == c) {// ???????¨??????¨???
					// ????????¨??????????¨?
					for (int count5 = 0; count5 < (c+1); count5++) {
						sum = sum + num[count3][count5];
					}
					sb.append(sum);
					sb.append("\n");
					sum = 0;// sum????????????
				} else {// ??????????????¨???
					sb.append(num[count3][count4]);
					sb.append(" ");
				}
			}
			sum = 0;//sum ????????????
			if(count3 == r){//???????¨??????¨???
				for(int count6=0;count6<c;count6++){
					for(int count7=0;count7<r;count7++){
						sum = sum + num[count7][count6];
					}
					sum2 = sum2 + sum;
					sb.append(sum);
					sb.append(" ");
					sum = 0;//sum????????????
				}
			}
		}
		sb.append(sum2);
		System.out.println(new String(sb));

	}
} Main

import java.util.*;

public class Main {
	public static void main(String args[]) {
		Scanner scan = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int r, c, sum,sum2;
		r = scan.nextInt();
		c = scan.nextInt();
		sum = 0;// ????¨????
		sum2 = 0;//??¨??????????¨????
		// ????¬???????????????????
		int num[][] = new int[r + 1][c + 1];
		// ?????????????´?
		for (int count = 0; count < r; count++) {
			for (int count2 = 0; count2 < c; count2++) {
				num[count][count2] = scan.nextInt();
			}
		}
		
		// ?¨??????¨StringBuilder???????´??????????????????? r=1 c=8
		for (int count3 = 0; count3 < (r + 1); count3++) {
			for (int count4 = 0; count4 < (c + 1); count4++) {
				if(count3 == r){break;}//????????????????????????????????????
				if (count4 == c) {// ???????¨??????¨???
					// ????????¨??????????¨?
					for (int count5 = 0; count5 < (c+1); count5++) {
						sum = sum + num[count3][count5];
					}
					sb.append(sum);
					sb.append("\n");
					sum = 0;// sum????????????
				} else {// ??????????????¨???
					sb.append(num[count3][count4]);
					sb.append(" ");
				}
			}
			sum = 0;//sum ????????????
			if(count3 == r){//???????¨??????¨???
				for(int count6=0;count6<c;count6++){
					for(int count7=0;count7<r;count7++){
						sum = sum + num[count7][count6];
					}
					sum2 = sum2 + sum;
					sb.append(sum);
					sb.append(" ");
					sum = 0;//sum????????????
				}
			}
		}
		sb.append(sum2);
		System.out.println(new String(sb));

	}
}