const input = require('fs').readFileSync('/dev/stdin', 'utf8').split(/\n/);
let [r, c] = input[0].split(' ').map(Number)
let num = [];
for (let i = 1; i < input.length; i++) {
    let tmp = input[i].split(' ').map(Number);
    num.push(tmp);
}
for (i = 0; i < r; i++) {
    let sum = num[i].reduce((a,x) => a+=x,0);
    num[i].push(sum);
    console.log(num[i].join(' '));
}

let sumLine = [];
for (i = 0; i <= c; i++) {
    let tmp = 0;
    for (let j = 0; j < r; j++) {
         tmp = tmp + num[j][i];
    }
    sumLine.push(tmp);
}
    console.log(sumLine.join(' '));
