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

spreadsheet = []
i = 0
while true do
   if i == r
      break
   end
   spreadsheet << gets.split.map(&:to_i)
   i += 1
end

spreadsheet.each do |line|
   sum = 0
   line.each do |val|
      sum += val
   end
   line << sum
end

new_line = []
c = c + 1
c.times do |i|
   new_sum = 0
   r.times do |j|
      new_sum += spreadsheet[j][i]
   end
   new_line << new_sum
end
spreadsheet << new_line

spreadsheet.each do |spread|
   spread.each do |num|
      print "#{num} "
   end
   puts
end