//fd = 0, nl = '\r\n'; // win
fd = '/dev/stdin', nl = '\n'; linux

line = require('fs').readFileSync(fd, 'ascii').split(nl);
line = line.map(function (line) {
	if (line.length === 0) return [];
	return line.split(' ').map(Number);
});

line.push([]);
r = line[0][0];

function add(a, b) {
	return a + b;
}
line[1].push(line[1].reduce(add, 0));
sum = [].concat(line[1]);

function addToSum(elem, i, line) {
	sum[i] += elem;
}
for (i = 2; i <= r; i++) {
	line_i = line[i];
	line_i.push(line_i.reduce(add, 0));
	line_i.forEach(addToSum);
}

line[r + 1] = sum;

function join(ary) { return ary.join(' '); }
console.log(line.map(join).join('\n'));