r, c = gets.chomp.split(" ").map(&:to_i)
spreadsheet = []
r.times do |i|
  spreadsheet[i] = gets.chomp.split(" ").map(&:to_i)
end
spreadsheet.each do |line|
  line.push line.inject(&:+)
end
spreadsheet.push spreadsheet.transpose.map { |e| e.inject(&:+) }
spreadsheet.each do |line|
  puts line.join(" ")
end