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

a = r.times.map { gets.split.map(&:to_i) }
a.each { |line| line.push line.sum }

bottom = (0..c).map do |col|
  r.times.map { |row| a[row][col] }.sum
end

a.push bottom

a.each do |line|
  puts line.join(' ')
end

