# 1 ? r, c ? 100
# (r + 1) × (c + 1) 

require 'matrix'

r, c = gets.split.map(&:to_i)
ary = []

ARGF.each do |line|
  ary << line.split.map(&:to_i)
end

# puts Matrix[ary]
# column 列ベクトル

table = Matrix[ary]


c.times do |i|
  col = table.column(i).to_a[0].each
  print col.join("\s")
  print "\s"
  puts  col.inject(:+)
end

r.times do |i|
  rows = table.row(0).nject(0){|m, row| m + row[i]}
  print "\s"
end
 
puts table.inject(:+).inject(:+)