r, c = gets.chomp.split.map(&:to_i)
last_line = Array.new(c + 1, 0)

r.times do
  columns = gets.chomp.split.map(&:to_i)
  columns.push(columns.sum)
  puts columns.join(" ")
  (0..c).each do |i|
    last_line[i] += columns[i]
  end
end

puts last_line.join(" ")

