r, c = gets.split.map(&:to_i)
last = Array.new( c, 0 )
r.times do
  line = gets.split.map(&:to_i)
  puts line.join(" ")+" "+line.inject(:+).to_s
  last = last.zip( line ).map{ | x, y | x + y }
end
puts last.join(" ")+" "+last.inject(:+).to_s