r, c = gets.split.map(&:to_i)
vec = []
total = 0

r.times do
  row = gets.split.map(&:to_i)
  row_sum = row.inject(:+)
  total += row_sum
  vec << [*row, row_sum]
end

last_row = Array.new(c, 0)

c.times do |j|
  vec.each do |v|
    last_row[j] += v[j]
  end
end

last_row << total

vec.each do |v|
  puts v.join(' ')
end

puts last_row.join(' ')

