r, c = gets.chomp.split(/\s/).map(&:to_i)
array = []

r.times do
  tmp = gets.chomp.split(/\s/).map(&:to_i)
  tmp << tmp.inject(:+)
  array << tmp
end

array.each do |row|
  puts row.join("\s")
end

puts array.transpose.map{ |column| column.inject(:+) }.join("\s")