#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(void) {
	int r,c,temp;
	cin >> r >> c;
	vector<int> sum(c+1,0);
	for (int i = 0; i < r; ++i) {
		int rsum = 0;
		for (int j = 0; j < c; ++j) {
			cin >> temp;
			sum[j] += temp;
			rsum += temp;
			cout << temp << " ";
		}
		cout << rsum <<  endl;
		sum[c] += rsum;
	}
	for (int j = 0; j < c+1; ++j) {
		cout << sum[j];
		if (j < c) {
			cout << " ";
		}
	}
	cout << endl;
	return 0;
}

