while line = gets
  n = line.to_i
  break if n.zero?

  data = n.to_i.times.map{ gets.split.map(&:to_i) }
  data.each{|row| row << row.inject(:+) }
  data = data.transpose
  data.each{|row| row << row.inject(:+) }
  data = data.transpose

  format = data.first.map{|n| "%5d" }.join
  data.each{|row| puts sprintf(format, *row)}

end