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

matrix = Array.new()

r.times do
    number = gets.split.map(&:to_i)
    number.push(number.inject(:+))
    matrix.push(number)
end

sum = Array.new(c + 1, 0)
matrix.each do |number|
    puts number.join "\s"
    sum = sum.zip(number).map { |a, b| a + b }
end

puts sum.join "\s"