r, c = gets.chomp.split.map(&:to_i)

a = []
r.times do
  a << gets.chomp.split.map(&:to_i)
end

a.each do |a_i|
  a_i.push(a_i.sum)
end

a << []

(c + 1).times do |i|
  sum = 0
  r.times do |j|
    sum += a[j][i]
  end
  a.last.push(sum)
end

a.each do |a_i|
  puts a_i.join(' ')
end

