def main
  while rows = gets.chomp.to_i and rows != 0
    matrix = []
    (1..rows).each do |row|
      nums = gets.chomp.split(/\s/).collect { |x| x.to_i}
      matrix << nums
    end
    solve(matrix, rows)
  end
end


def solve(matrix, n)
  matrix.each do |row|
    sum = row.inject(0) { |s, e| s + e}
    row << sum
  end
  sums = []
  (0..n).each do |col|
    tmp = 0
    (0..n-1).each do |row|
      tmp += matrix[row][col]
    end 
    sums << tmp
  end
  matrix << sums

  matrix.each do |row|
    puts row.collect { |x| ("%5d" % x).to_s }.join("")
  end
end

main