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

sheet = Array.new(r + 1){Array.new(c + 1, 0)}

r.times do |i|
  sheet[i] = gets.split.map(&:to_i)
  sheet[i][c] = sheet[i].inject{|sum, a| sum + a}
  (c + 1).times do |j|
    sheet[r][j] += sheet[i][j]
  end
  puts sheet[i].join(" ")
end
puts sheet[r].join(" ")