var input = ""
process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', function(chunk) {
  input += chunk;
});
 
process.stdin.on('end', function() {
  input = input.split("\n");
  i = 0;
  var r = input[i].split(" ")[0];
  var c = input[i].split(" ")[1];

  var sum = new Array(c);
  for (j = 0; j < c; j++) {
    sum[j]  = 0;
  }
  var total_all = 0;

  for (i = 1; i <= r; i++) {
    var arr = input[i].split(" ");
    var total = 0;
    for (j = 0; j < c; j++) {
      n = parseInt(arr[j], 10);
      total += n;
      sum[j] += n;
      total_all += n;
    }
    arr.push(total);
    console.log(arr.join(" "));
  }
  sum.push(total_all);
  console.log(sum.join(" "));
});