num = []
r, c = gets.split.map(&:to_i)
r.times do
  line = gets.split.map(&:to_i)
  line << line.reduce(:+)
  num << line
  puts line.join(" ")
end
p num
a = (0..c).to_a.map do |j|
  (0...r).to_a.map { |i| num[i][j] }.reduce(:+)
end
puts a.join(" ")