loop do
   n = gets.to_i
   break if n == 0
   table = []
   n.times do
      row = gets.split(" ").map{|x| x.to_i}
      row.push row.inject{|m,x| m += x}
      table.push row
   end
   colsum = Array.new n + 1,0
   table.each do |row|
      row.each_with_index do |x,i|
         colsum[i] += x
      end
   end
   table.push colsum
   table.each do |row|
      row.each do |x|
         printf "%5d",x
      end
      puts ""
   end
end