r, c = gets.chomp.split.map(&:to_i)
b = 0
d = Array.new()
e = 0
r.times do
    a = gets.chomp.split
    a.each do |i|
        print "#{i} "
        b += i.to_i
    end
    puts "#{b}"
    e += b
    b = 0
    d << a
end
i = 0
d.transpose.each do |j|
    j.each do |k|
        i += k.to_i
    end
    print "#{i} "
    i = 0
end
print "#{e}"
puts ""
#e = d.transpose
#puts "#{e}"
#puts ""