m, n = gets.split(" ").map(&:to_i)
sum = Array.new(n + 1, 0)

m.times do
  r = gets.split(" ").map(&:to_i)
  r.push(r.sum)
  for i in 0..n
    sum[i] += r[i]
  end
  puts r.join(" ")

end
puts sum.join(" ")

