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

r.times do
  x << gets.chomp.split.map(&:to_i)
end

xl = []
(0...c).each do |i|
  n = 0
  (0...r).each do |j|
    n += x[j][i]
  end
  xl << n
end
xl << xl.inject(:+)

x.each do |a|
  print a.join(' ') , ' ' , a.inject(:+)
  puts
end
puts xl.join(' ')