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

a = []
r.times do |i|
    a[i] = gets.split(" ").map(&:to_i)
end

x = Array.new(c + 1, 0)
a.each do |b|
    print b.join(" "), " ", b.sum, "\n"
    c.times do |i|
        x[i] += b[i]
    end
    x[c] += b.sum
end

print x.join(" "), "\n"

