require 'matrix'
r, c = gets.chomp.split(' ').map(&:to_i)
colsum = Vector.elements(Array.new(c+1, 0))
while line = gets
  row = line.chomp.split(' ').map(&:to_i)
  rowsum = row.inject(:+)
  row << rowsum
  colsum += Vector.elements(row)
  print row.join(' '), "\n"
end
print colsum.to_a.join(' '), "\n"