r,c = gets.split.map(&:to_i)
row_array =[[]]
sum_column = Array.new(c,0)
sum_all = 0
r.times do |i|
  sum_row = 0 
  row_array[i] = gets.split.map(&:to_i)
  count = 0
  row_array[i].each{|a| sum_row += a; print("#{a} "); sum_column[count] += a; count += 1 }
  puts sum_row
  sum_all += sum_row
end
sum_column.each{|a| print("#{a} ")}
puts sum_all

