row=[]
r,c=gets.split.map(&:to_i)
a = r.times.map { gets.split.map(&:to_i) }
total = a.sum{|x| x.sum}
a.transpose.each { |u| row<<u.sum }
row << total
a.each { |u| u<<u.sum }

a.each { |b| puts b.join(' ') }
puts row.join(' ')

