import functools



n = int(eval(input()))

while(n):

    bottom_record = [0]*(n+1)



    for i in range(0, n):

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

        l.append(functools.reduce(lambda x, y: x + y, l))

        bottom_record = [x + y for(x,y) in zip(bottom_record, l)]

        print((''.join(map('{0:5d}'.format, l)))) 

    print((''.join(map('{0:5d}'.format, bottom_record))))

    n = int(eval(input()))
n = int(eval(input()))

while(n):

    bottom_record = [0]*(n+1)



    for i in range(0, n):

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

        l.append(sum(l))

        bottom_record = [x + y for(x,y) in zip(bottom_record, l)]

        print((''.join(map('{0:5d}'.format, l)))) 

    print((''.join(map('{0:5d}'.format, bottom_record))))

    n = int(eval(input()))
import functools

n = int(eval(input()))
while n:
    bottom_record = [0] * (n + 1)
    for i in range(0, n):
        l = list(map(int, input().split()))
        l.append(functools.reduce(lambda x, y: x + y, l))
        bottom_record = [x + y for (x, y) in zip(bottom_record, l)]
        print(("".join(map("{0:5d}".format, l))))
    print(("".join(map("{0:5d}".format, bottom_record))))
    n = int(eval(input()))

n = int(eval(input()))
while n:
    bottom_record = [0] * (n + 1)
    for i in range(0, n):
        l = list(map(int, input().split()))
        l.append(sum(l))
        bottom_record = [x + y for (x, y) in zip(bottom_record, l)]
        print(("".join(map("{0:5d}".format, l))))
    print(("".join(map("{0:5d}".format, bottom_record))))
    n = int(eval(input()))

-import functools - -        l.append(functools.reduce(lambda x, y: x + y, l)) +        l.append(sum(l))