require 'matrix'

$stdin = DATA if $stdin.eof?

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

a = []
ARGF.each do |line|
  a << line.split.map(&:to_i)
end

table = Matrix[a]

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

c.times do |i|
  print table.row(0).inject(0){|m, row| m + row[i]}
  print "\s"
end

puts table.inject(:+).inject(:+)