row, column = gets.chomp.split.map(&:to_i)
table = row.times.map{ gets.chomp.split.map(&:to_i) }

table.each do |line| 
  line << line.sum
  puts line.join(' ')
end

puts (column+1).times.map { |x|
  row.times.reduce(0) { |t, y|
    t + table[y][x]
  }
}.join(' ')

