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

src_array = r.times.map do |_|
  gets.split.map(&:to_i)
end

sum_row = c.times.map do |i|
  r.times.reduce(0) do |sum, j|
    sum += src_array[j][i]
  end
end

dist_array = src_array << sum_row
dist_array.each do |row|
  puts (row << row.sum).join(' ')
end
