r, c = gets.split.map(&:to_i)
T = Array.new(r).map{Array.new(c+1,0)}
row = Array.new(c+1,0)
r.times do |i|
    x = gets.split.map(&:to_i)
    x.push x.inject(:+)
    x.length.times {|j| row[j] += x[j]}
    T[i] = x
end
T.push row
T.each do |t|
    puts t.join(' ')
end
