process.stdin.setEncoding('ascii');
process.stdin.on('data', function (chunk) {

	var line, r, sum, i, line_i;

	line = chunk.split(/\r?\n/).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'));

});