a, b = gets.split(" ").map(&:to_i)
ss = Array.new(a + 1){Array.new(b + 1, 0)}
a.times do |i|
    ary = gets.split(" ").map(&:to_i)
    ss[i][0, b] = ary
    ss[i][b] = ary.inject(:+)
end
ss[a] = ss.transpose.map{|j| j.inject(:+)}
(a + 1).times do |k|
    puts ss[k].join(" ")
end