r, c = gets.split.map(&:to_i)
m = []
r.times {m << gets.split.map(&:to_i)}
tate = Array.new(c, 0)
putout = ->(ar) {
  st = ""
  sum = 0
  ar.each_with_index do |x, i|
    st += x.to_s + " "
    sum += x
    tate[i] += x
  end
  st += sum.to_s
  puts st
}
m.each {|x| putout.(x)}
putout.(last)
