
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));

	}
}
/tmp/hf-datasets-cache/medium/datasets/42704881117311-config-parquet-and-info-qiankunmu-Project_CodeNet-896c9d36/hub/datasets--qiankunmu--Project_CodeNet_Python800_and_Java250/snapshots/c899fde44a77ba1cc14973ed3de064e4ff3aadc7/Project_CodeNet_Java250.tar.gz