  require "matrix"
  iR,iC=$stdin.gets.split.map(&:to_i)
  arr=[]
  iR.times do |n|
    arr<<$stdin.gets.split.map(&:to_i)
  end
  mx=Matrix.rows(arr)
  total=0
  iR.times do |n|
    r=mx.row(n).to_a
    print r.join(" ") + " " + r.sum.to_s + "\n"
    total+=r.sum
  end
  iC.times do |n|
    c=mx.column(n).to_a
    print c.sum.to_s + " "
  end
  puts total
