loop{
  n = gets.to_i
  break if n == 0

  matrix = Array.new(n)
  (0..n-1).each do |i|
    matrix[i] = gets.split.map(&:to_i)
  end

  matrix.each do |line|
    line_sum = line.inject(&:+)
    line.push(line_sum)

    line.each{|elm| print(sprintf("%5d", elm)) }
    puts
  end

  matrix.transpose.each do |col|
    col_sum = col.inject(&:+)
    print(sprintf("%5d", col_sum))
  end
  puts
}