while True:

    n = int(input())

    if n == 0:

        break

    row = n

    tab = []

    for i in range(row):

        r = list(map(int, input().split()))

        r.append(sum(r))

        tab.append(r)



    bel = []

    for i in range(row):

        tmp = 0

        for j in range(row):

            tmp += tab[j][i]

        bel.append(tmp)

    bel.append(sum(bel))

    tab.append(bel)



    for i in range(row+1):

        for j in range(row+1):

            #print("".join(map("{0:>5}".format, str(tab[i][j]))))

            print('{:>5}'.format(str(tab[i][j])), end='')

        print()

def form(x, e=""):

    print("{:>5}".format(x), end=e)



while True:

    n = int(input())

    if n == 0:

        break

    rows = [list(map(int, input().split())) for _ in range(n)]

    for row in rows:

        for r in row:

            form(r)

        form(sum(row), "\n")

    columns = list(zip(*rows))

    for column in columns:

        form(sum(column))

    form(sum(sum(row) for row in rows), "\n")

while True:
    n = int(input())
    if n == 0:
        break
    row = n
    tab = []
    for i in range(row):
        r = list(map(int, input().split()))
        r.append(sum(r))
        tab.append(r)
    bel = []
    for i in range(row):
        tmp = 0
        for j in range(row):
            tmp += tab[j][i]
        bel.append(tmp)
    bel.append(sum(bel))
    tab.append(bel)
    for i in range(row + 1):
        for j in range(row + 1):
            # print("".join(map("{0:>5}".format, str(tab[i][j]))))
            print("{:>5}".format(str(tab[i][j])), end="")
        print()

def form(x, e=""):
    print("{:>5}".format(x), end=e)


while True:
    n = int(input())
    if n == 0:
        break
    rows = [list(map(int, input().split())) for _ in range(n)]
    for row in rows:
        for r in row:
            form(r)
        form(sum(row), "\n")
    columns = list(zip(*rows))
    for column in columns:
        form(sum(column))
    form(sum(sum(row) for row in rows), "\n")

+def form(x, e=""): +    print("{:>5}".format(x), end=e) + + -    row = n -    tab = [] -    for i in range(row): -        r = list(map(int, input().split())) -        r.append(sum(r)) -        tab.append(r) -    bel = [] -    for i in range(row): -        tmp = 0 -        for j in range(row): -            tmp += tab[j][i] -        bel.append(tmp) -    bel.append(sum(bel)) -    tab.append(bel) -    for i in range(row + 1): -        for j in range(row + 1): -            # print("".join(map("{0:>5}".format, str(tab[i][j])))) -            print("{:>5}".format(str(tab[i][j])), end="") -        print() +    rows = [list(map(int, input().split())) for _ in range(n)] +    for row in rows: +        for r in row: +            form(r) +        form(sum(row), "\n") +    columns = list(zip(*rows)) +    for column in columns: +        form(sum(column)) +    form(sum(sum(row) for row in rows), "\n")