Problem:
Matrix-like Computation Your task is to develop a tiny little part of spreadsheet software. Write a program which adds up columns and rows of given table as shown in the following figure:   Input The input consists of several datasets. Each dataset consists of:  n (the size of row and column of the given table)
1st row of the table
2nd row of the table
 :
 : n th row of the table The input ends with a line consisting of a single 0. Output For each dataset, print the table with sums of rows and columns. Each item of the table should be aligned to the right with a margin for five digits. Please see the sample output for details. Sample Input 4
52 96 15 20
86 22 35 45
45 78 54 36
16 86 74 55
4
52 96 15 20
86 22 35 45
45 78 54 36
16 86 74 55
0 Output for the Sample Input 52   96   15   20  183
   86   22   35   45  188
   45   78   54   36  213
   16   86   74   55  231
  199  282  178  156  815
   52   96   15   20  183
   86   22   35   45  188
   45   78   54   36  213
   16   86   74   55  231
  199  282  178  156  815

Solution (Python):
import sys
while True:
  a=int(input())
  if a==0: break
  soug,tt,te=[0]*a,[],0
  for i in range(a):
    hoge=map(int,raw_input().split())
    te=0
    for j in range(a):
      te+=hoge[j]
      soug[j]+=hoge[j]
      sys.stdout.write("%5d"%hoge[j])
    else: sys.stdout.write("%5d"%te) ; print""
  else:
    te=0
    for i in range(a):
      sys.stdout.write("%5d"%soug[i])
      te+=soug[i]
    else: sys.stdout.write("%5d"%te)   ; print""

Solution (Python):
while 2 > 1:
	line_num = int(raw_input())
	if line_num < 1:
		break
	mylist = [0,0,0,0,0,0,0,0,0,0,0]
	for n in range(line_num):
		dat = map(int, raw_input().split(" "))
		col_num = len(dat)
		row_sum = 0
		for m in range(col_num):
			mylist[m] += dat[m]
			row_sum += dat[m]
		mylist[col_num] = row_sum
		disp = ""
		for m in range(col_num):
			disp += ("%s" % dat[m]).rjust(5)
		disp += ("%s" % row_sum).rjust(5)
		print disp
	mylist[col_num] = 0
	for m in range(col_num):
		mylist[col_num] += mylist[m]
	disp_sum = ""
	for m in range(col_num + 1):
		disp_sum += ("%s" % mylist[m]).rjust(5)
	print disp_sum

Solution (Python):



import sys

def solve():
    while True:
        n = input()
        if n > 0:
            mat = [[0 for i in xrange(n + 1)] for j in xrange(n + 1)]
            for i in range(n):
                line = sys.stdin.readline().split()
                for j in range(n):
                    mat[i][j] = int(line[j])
            for i in xrange(n):
                mat[i][n] = sum(mat[i][j] for j in range(n))
                mat[n][i] = sum(mat[j][i] for j in range(n))
            mat[n][n] = sum(mat[n][i] for i in range(n))
            for lst in mat:
                print "".join(map(lambda n: "%5d" % n, lst))
        else:
            sys.exit()

if __name__ == "__main__":
    solve()

Solution (Python):
import sys
R = lambda:map(int,raw_input().split())

while 1:
    n = int(raw_input())
    if n == 0: break
    lst = []
    for i in xrange(n):
        lst.append(R())
        lst[i].append(sum(lst[i]))
    s = []
    for i in xrange(n+1):
        t = 0
        for j in xrange(n):
            t += lst[j][i]
        s.append(t)

    for i in xrange(n):
        for j in xrange(n+1):
            lst[i][j] = str(lst[i][j]).rjust(5)
    for i in xrange(n+1):
        s[i] = str(s[i]).rjust(5)
    lst.append(s)
    
    for i in xrange(n+1):
        print "".join(lst[i])

Solution (Python):
import sys

while True:
    n = int(raw_input())
    if n == 0: break
    
    a = []
    for i in range(n):
        a.append(map(int, raw_input().split()))
    
    for i in range(n):
        a[i].append(sum(a[i]))
    
    col_sum = []
    for col in range(len(a[0])):
        s = 0
        for row in range(len(a)):
            s += a[row][col];
        col_sum.append(s)
    a.append(col_sum)

    for row in range(len(a)):
        for col in range(len(a[0])):
            sys.stdout.write("%5d" % a[row][col])
        print

Solution (Python):
while True:
    n=input()
    if n==0:
        break
    li=[[0]*(n+1) for unused in xrange(n+1)]
    for i in xrange(n):
        li[i][:n]=map(int,raw_input().split())
    for i in xrange(n):
        li[i][n]=sum(li[i])
        li[n][i]=sum([li[j][i] for j in xrange(n)])
    li[n][n]=sum(li[n])
    print "\n".join(["".join(map(lambda x:str(x).rjust(5),li[i])) for i in xrange(n+1)])

Solution (Python):
def print_nums(nums):
    print ''.join(['%5d' % num for num in nums])

while 1:
    n = int(raw_input())
    if n == 0: break
    sums = [0] * (n + 1)
    for i in range(n):
        nums = map(int, raw_input().split())
        nums.append(sum(nums))
        print_nums(nums)
        for j in range(n + 1):
            sums[j] += nums[j]
    print_nums(sums)

Solution (Python):

ans=[]
while True:
    curAns=[]
    n=input()
    if n ==0:
        break
    for i in xrange(n):
        lst=map(int,raw_input().split())
        lst.append(sum(lst))
        curAns.append(lst)
    lLst=[]                             
    for i in xrange(n+1):               
        verSum=0
        for j in xrange(n):             
            verSum+=curAns[j][i]
        lLst.append(verSum)
    curAns.append(lLst)
    relAns=[]
    for i in curAns:
        tmpLst=map(str,i)
        nLst=[]
        for j in tmpLst:
            nLst.append(j.rjust(5))
        ansStr=''.join(nLst)
        relAns.append(ansStr)
    ans.append(relAns)

for i in ans:
    for j in i:
        print j

Solution (Python):
import sys
f=lambda v,b: v.append(sum(b))
while True:
    c=input()
    if c==0: break
    x=range(c)
    z=range(c+1)
    q={}
    for i in x:
        q[i]=map(int,raw_input().split())
        t=q.values()[i]
        f(t,t)
    q[i+1]=[]
    for g in x:
        w=[]
        for j in x:
            w.append(q.values()[j][g]) 
        f(q[i+1],w)
    t=q[i+1]
    f(t,t)
    for i in q.values():
        for j in z:
            sys.stdout.write("%5d"%i[j])
        print

Solution (Python):
import sys
f=lambda v,b: v.append(sum(b))
while True:
    c=input()
    if c==0: break
    x=range(c);z=range(c+1);q={}
    for i in x:
        q[i]=map(int,raw_input().split());t=q.values()[i];f(t,t)
    q[i+1]=[]
    for g in x:
        w=[]
        for j in x:
            w.append(q.values()[j][g]) 
        f(q[i+1],w)
    t=q[i+1];f(t,t)
    for i in q.values():
        for j in z: sys.stdout.write("%5d"%i[j])
        print

Solution (Python):

import sys


def solv(matrix):
    for row in matrix:
        s = sum(row)
        row.append(s)
    r = []
    l = len(matrix[0])
    for c in range(l):
        column = map(lambda x: x[c], matrix)
        sc = sum(column)
        r.append(sc)
    matrix.append(r)
    return matrix


while True:
    n = int(sys.stdin.readline())
    if n == 0:
        exit()
    matrix = []
    for i in range(n):
        line = sys.stdin.readline()
        row = map(int, line.split(' '))
        matrix.append(row)
    matrix2 = solv(matrix)
    for row in matrix2:
        print ''.join(map(lambda x: str(x).rjust(5), row))

Solution (Python):
while True:
    n = int(raw_input())
    if n == 0:
        break

    a = [[] for i in range(n + 1)]

    for i in range(n + 1):
        if i < n:
            a[i] = map(int, raw_input().split())
            a[i].append(sum(a[i]))
        else:
            a[i] = [sum([a[j][k] for j in range(n)]) for k in range(n + 1)]

    s = ""
    for i in range(n + 1):
        for j in range(n + 1):
            s += "{0:5d}".format(a[i][j])
        s += "\n"
    print s,

Solution (Python):
while True:
    n = int(raw_input())
    if n == 0:
        break

    a = [[] for i in range(n + 1)]
    for i in range(n + 1):
        if i < n:
            a[i] = map(int, raw_input().split())
            a[i].append(sum(a[i]))
        else:
            a[i] = [sum([a[j][k] for j in range(n)]) for k in range(n + 1)]

    s = ""
    for i in range(n + 1):
        s += "".join(map(lambda x: str(x).rjust(5, " "), a[i])) + "\n"
    print s,

Solution (Python):
while True:
    n = int(raw_input())
    if n == 0:
        break

    a = [[] for i in range(n + 1)]
    for i in range(n + 1):
        if i < n:
            a[i] = map(int, raw_input().split())
            a[i].append(sum(a[i]))
        else:
            a[i] = [sum([a[j][k] for j in range(n)]) for k in range(n + 1)]

    for i in range(n + 1):
        print "".join(map(lambda x: str(x).rjust(5, " "), a[i])) + "\n",

Solution (Python):
while True:
    n = int(raw_input())
    if n == 0:
        break

    a = []
    for i in range(n + 1):
        if i < n:
            a.append(map(int, raw_input().split()))
            a[i].append(sum(a[i]))
        else:
            a.append([sum([a[j][k] for j in range(n)]) for k in range(n + 1)])

    for aa in a:
        print ''.join(map(lambda x: str(x).rjust(5, ' '), aa)) + '\n',

Solution (Python):
import sys

def solve():
    while True:
        n = input()
        if n > 0:
            mat = [[0 for i in xrange(n + 1)] for j in xrange(n + 1)]
            for i in range(n):
                line = sys.stdin.readline().split()
                for j in range(n):
                    mat[i][j] = int(line[j])
            for i in xrange(n):
                mat[i][n] = sum(mat[i][j] for j in range(n))
                mat[n][i] = sum(mat[j][i] for j in range(n))
            mat[n][n] = sum(mat[n][i] for i in range(n))
            for lst in mat:
                print "".join(map(lambda n: "%5d" % n, lst))
        else:
            sys.exit()

if __name__ == "__main__":
    solve()

Solution (Python):

from __future__ import division, print_function
from sys import stdin, exit


def main(readline=stdin.readline):
    fmt = '{:5d}'.format

    while 1:
        n = int(readline())
        if not n:
            exit()

        total = [0] * (n+1)

        for _ in range(n):
            s = 0
            for i, val in enumerate(int(d) for d in readline().split()):
                total[i] += val
                s += val
                print(fmt(val), end='')
            total[-1] += s
            print(fmt(s))

        print(''.join(fmt(i) for i in total))


if __name__ == '__main__':
    main()

Solution (Python):
n = int(raw_input())
while n != 0:
	a = [map(int,raw_input().split()) for i in xrange(n)]
	a = [l + [sum(l)] for l in a]
	a.append([sum([l[i] for l in a]) for i in xrange(len(a[0]))])
	print '\n'.join([''.join([str(i).rjust(5) for i in l]) for l in a])
	n = int(raw_input())

Solution (Python):
while True:
	n = int(raw_input())
	if n == 0: break
	ls = [map(int, raw_input().split()) for i in range(n)]+[[0]*(n+1)]
	for i in range(n):
		ls[i].append(sum(ls[i]))
	for i in range(n+1):
		for j in range(n):
			ls[n][i] += ls[j][i]
	for row in ls:
		print ''.join(map(lambda x: str(x).rjust(5), row))

Solution (Python):
while True:
	n = input()
	if n == 0: break
	ls = [map(int, raw_input().split())+[0] for i in range(n)]+[[0]*(n+1)]
	for row in range(n+1):
		for col in range(n):
			ls[n][col] += ls[row][col] if row != n else 0
			ls[row][n] += ls[row][col]
		print "".join(map(lambda x: str(x).rjust(5), ls[row]))

Solution (Python):
while 1:
	n = input()
	if n == 0: break
	l = [map(int, raw_input().split())+[0] for i in range(n)]+[[0]*(n+1)]
	for r in range(n+1):
		for c in range(n):
			l[n][c] += l[r][c] if r != n else 0
			l[r][n] += l[r][c]
		print "".join(map(lambda x: str(x).rjust(5), l[r]))

Solution (Python):
while True:
    n = input()
    ans = []
    if n == 0: break
    tmp = [0] * (n+1)
    for i in range(n):
        ans.append(map(int, raw_input().split()))
        ans[i].append(sum(ans[i]))
        for j in range(len(ans[i])):
            tmp[j] += ans[i][j]
    ans.append(tmp)
    for a in ans:
        print "".join(map(lambda x: str(x).rjust(5), a))

Solution (Python):
while True:
    A = []
    n = int(input())
    if n == 0:
        break
    for i in range(n):
        A.append(list(map(int,input().split())))
    result = A[:]
    result_T = list(map(list,zip(*A)))
    for line in result:
        line.append(sum(line))
        
    for line in result_T:
        line.append(sum(line))

    result_T = list(map(list,zip(*result_T)))
    result.append(result_T[-1]+[sum(result_T[-1])])
    
    for i in range(len(result)):
        for j in range(len(result[i])):
            print(str(result[i][j]).rjust(5),end="")
        print()

Solution (Python):
while True :
    n = int(input())
    if n == 0 :
        break
    
  
    L = [0] * n
    for i in range(n) :
        tmp = list(map(int, input().split()))
        for j in range(n) :
            L[j] += tmp[j]
        tmp.append(sum(tmp))
        for j in range(n+1) :
            print(str(tmp[j]).rjust(5), end = "")
        print()
    L.append(sum(L))
    for j in range(n+1) :
        print(str(L[j]).rjust(5), end = "")
    print()

Solution (Python):
import sys
readline = sys.stdin.readline
write = sys.stdout.write

fmt = "{:5d}".format
def solve():
    N = int(readline())
    if N == 0:
        return False
    A = [list(map(int, readline().split())) for i in range(N)]
    for ln in A:
        write("".join(map(fmt, ln)))
        write(fmt(sum(ln)))
        write("\n")
    b = [sum(A[i][j] for i in range(N)) for j in range(N)]
    write("".join(map(fmt, b)))
    write(fmt(sum(b)))
    write("\n")
    return True
while solve():
    ...

Solution (Python):
while(1):
    n = int(input())
    if n == 0:
        break
    t = [[0] * (n + 1) for i in range(n + 1)]
    m = []
    for i in range(0,n):
        m.append(list(int(x) for x in input().split(" ")))
    sum = 0
    for i in range(0,n):
        sum1 = 0
        sum2 = 0
        for j in range(0,n):
            t[i][j] = m[i][j]
            sum1 += m[i][j]
            sum2 += m[j][i]
        t[i][n] = sum1
        t[n][i] = sum2
        sum += sum1
    t[n][n] = sum
    
    
    for i in range(0,n + 1):
        for j in range(0,n + 1):
            print("%5d" %(t[i][j]),end="")
        print()

Solution (Python):
while True:
    n=int(input())
    if n==0:
        break
    i=0
    mat=[]
    
    while i<n:
        tmp_mat=list(map(int, input().split()))
        mat.extend(tmp_mat)
        i+=1
    total=0
    tmp_last=[0 for l in range(n+1)]
    for x in range(len(mat)):
        total+=mat[x]
        tmp_last[x%n]+=mat[x]
        print(str(mat[x]).rjust(5),end="")
        if x%n==n-1:
            tmp_last[n]+=total
            print(str(total).rjust(5))
            total=0
    for y in range(n+1):
        print(str(tmp_last[y]).rjust(5),end="")
    print("")

Solution (Python):
import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop
import copy
from test.support import _MemoryWatchdog

BIG_NUM = 2000000000
HUGE_NUM = 99999999999999999
MOD = 1000000007
EPS = 0.000000001
sys.setrecursionlimit(100000)


SIZE = 51
table = [[None]*SIZE for _ in range(SIZE)]

while True:
    N = int(input())
    if N == 0:
        break

    for row in range(N+1):
        for col in range(N+1):
            table[row][col] = 0

    sum = 0

    for row in range(N):
        input_array = list(map(int,input().split()))
        for col in range(N):
            table[row][col] += input_array[col]
            table[N][col] += input_array[col]
            table[row][N] += input_array[col]
            sum += input_array[col]

    table[N][N] = sum

    for row in range(N+1):
        for col in range(N+1):
            print("%5d"%(table[row][col]),end="")
        print()

Solution (Python):
while True:
	n = int(input())
	if n == 0: break
	arr = [[0 for r in range(n+2)] for c in range(n+2)]

	for r in range(n):
		arr[r] = list(map(int, input().split()))
		arr[r].append(sum(arr[r]))

	for c in range(n+1):
		s = 0
		for r in range(n): s += arr[r][c]
		arr[n][c] = s

	for r in range(n+1):
		for c in range(n+1): print(format(arr[r][c], '5d'), end='')
		print()

Solution (Python):
while 1:
    n = int(input())
    if n == 0:
        break
    L = []
    for i in range(n):
        L += [list(map(int,input().split()))+[0]]
    L += [[0]*(n+1)]
    for r in range(n):
        for c in range(n):
            L[r][n] += L[r][c]
            L[n][c] += L[r][c]
            L[n][n] += L[r][c]
    for f in range(n+1):
        for h in range(n+1):
            L[f][h] = str(L[f][h]).rjust(5)
        print (''.join(L[f]))

Solution (Python):
while True:
    n = int(input())
    if n == 0:
        break
    matrix = [list(map(int, input().split())) for _ in range(n)]
    for i in range(n):
        sum_ = sum(matrix[i])
        print(str("{:>5d}" * (n+1)).format(*matrix[i], sum_))
    sum_ = [sum(e) for e in list(zip(*matrix))]
    print(str("{:>5d}" * (n+1)).format(*sum_, sum(sum_)))

Solution (Python):

while True:
    L = []
    n = int(input())
    if n == 0:
        break
    for _ in range(n):
        l = [int(x) for x in input().split()]
        l.append(sum(l))
        L.append(l)

    l = []
    for i in range(len(L[0])):
        s = []
        for j in range(len(L)):
            s.append(L[j][i])
        l.append(sum(s))
    L.append(l)

    for l in L:
        for i in l:
            print("{:>5d}".format(i),end="")
        print()

Solution (Python):
while True:
  n = int(input())
  if n==0:
    break
  A=[]
  for a in range(n):
    x =list(map(int, input().split()))
    x.append(sum(x))
    A.append(x)
  Y=[]
  for j in range(n+1):
    y=0
    for i in range(n):
      y+=A[i][j]
    Y.append(y)

  A.append(Y)
  
  for i in range(n+1):
    for j in range(n+1):
      a=A[i][j]
      a=str(a)
      a=a.rjust(5)
      print(a,end="")
    print()

Solution (Python):
while True:
    n = int(input())
    if n == 0:
        break
    arr = [[0 for r in range(n+2)]for c in range(n+2)]
    
    for r in range(n):
        arr[r] = list(map(int, input().split()))
        arr[r].append(sum(arr[r]))

    for c in range(n+1):
        s = 0
        for r in range(n):
            s += arr[r][c]
        arr[n][c] = s
    
    for r in range(n+1):
        for c in range(n+1):
            print(format(arr[r][c],'5d'), end ='')
        print()

Solution (Python):






import sys
import math


class mymath:
    
    pi = 3.14159265358979323846264338

    
    def pnum_eratosthenes(self, n):
        ptable = [0 for i in range(n+1)]
        plist = []

        for i in range(2, n+1):
            if ptable[i]==0:
                plist.append(i)
                for j in range(i+i, n+1, i):
                    ptable[j] = 1
        return plist

    
    def gcd(self, a, b):
        if b == 0:
            return a
        return self.gcd(b, a%b)

    
    def lcm(self, a, b):
        return (a*b)//self.gcd(a,b)

    
    def mul(self, A, B):
        ans = []
        for a in A:
            c = 0
            for j, row in enumerate(a):
                c += row*B[j]
            ans.append(c)
        return ans

mymath = mymath()


class output:
    
    def list(self, l):
        l = list(l)
        
        for i, num in enumerate(l):
            print(num, end="")
            if i != len(l)-1:
                print(" ", end="")
        print()

output = output()






































def get_input():
    N = []
    while True:
        try:
            N.append(input())
            
            
        except EOFError:
            break
    return N

def is_integer(n):
    try:
        float(n)
    except ValueError:
        return False
    else:
        return float(n).is_integer()

def dist(A, B):
    d = 0
    for i in range(len(A)):
        d += (A[i]-B[i])**2            
    d = d**(1/2)
    return d

while True:
    N = int(input())
    A = []

    if N == 0:
        break

    for i in range(N):
        A.append([int(x) for x in input().split()])

    for i in range(N):
        A[i].append(sum(A[i]))

    B = []

    for i in range(N+1):
        temp = 0
        for j in range(N):
            temp += A[j][i]
        B.append(temp)
    A.append(B)

    for row in A:
        for i in row:
            s = str(i)
            print(s.rjust(5, ' '), end='')
        print()
        pass

Solution (Python):
while True:
    n=int(input())
    if n==0:
        break
    
    ts=[0]*n
    ys=0
    a=[]
    for i in range(n):
        b=list(map(int,input().split()))
        s=sum(b)
        a.append(b+[s])
        for i in range(n):
            ts[i]+=b[i]
        ys+=s
    t=sum(ts)
    for i in range(n):
        for j in range(n+1):
            print(str(a[i][j]).rjust(5),end='')
        print()

    for i in range(n):
        print(str(ts[i]).rjust(5),end='')
    print(str(t).rjust(5))

Solution (Python):
while 1:
    try:
        N=int(input())
        if N==0:break
        matrix = [list(map(int,input().split())) for i in range(N)]
        matrix.append([])
        for i in range(N):matrix[i].append(sum(matrix[i]))
        for j in range(len(matrix)):
            pointer=0
            for k in range(N):
                pointer +=matrix[k][j]
            matrix[N].append(pointer)
        for i in range(N+1):
            for j in range(N+1):
                print(str(matrix[i][j]).rjust(5), end="")
            print()
    except:break

Solution (Python):
if __name__ == '__main__':

	while True:

		n = int(input())
		
		if n == 0:
			break

		A = [[0 for _ in range(n+1)]for _ in range(n)]
		B = [0 for _ in range(n+1)]

		for i in range(n):
			tmp = list(map(int,input().split()))
			
			for j,k in enumerate(tmp):
				A[i][j] += k
				B[j] += k

			A[i][n] = sum(A[i])
			B[n] = sum(B[:n])
		
		for m in A:
			tmp2 = ""
			for g in m:
				tmp2 += '{:>5}'.format(str(g))
			print(tmp2)
		tmp3 = ""
		for y in B:
			tmp3 += '{:>5}'.format(str(y))
		print(tmp3)

Solution (Python):
while 1:
    n=int(input())
    if n==0:break
    ans=[0]*(n+1)**2
    for i in range(0,n):
        keep=list(map(int,input().split()))
        s_keep=0
        for j in range(n):
            ans[i*(n+1)+j]=keep[j]
            s_keep+=keep[j]
        ans[(i+1)*(n+1)-1]=s_keep
    for i in range(n+1):
        s_keep=0
        for j in range(n+1):
            s_keep+=ans[i+j*(n+1)]
        ans[i+n*(n+1)]=s_keep
    for i in range(n+1):
        for j in range(n+1):
            print(f"{ans[i*(n+1)+j]:>5}",end="")
        print()

Solution (Python):
while 1:
    n = int(input())
    if n == 0:
        break

    mat = [[0 for i in range(n)] for j in range(n)]
    for i in range(n):
        mat[i] = list(map(int, input().split()))

    for i in range(n):
        tmp = 0
        for j in range(n):
            tmp += mat[i][j]
        mat[i].append(tmp)

    col = [0] * n
    for i in range(n):
        tmp = 0
        for j in range(n):
            tmp += mat[j][i]
        col[i] = tmp

    ans = sum(col)
    col.append(ans)

    for i in range(n):
        print(''.join(str(ans).rjust(5) for ans in mat[i]))

    print(''.join(str(ans).rjust(5) for ans in col))

Solution (Python):
while True:
    n = int(input())
    if n == 0:
        break
    ans = []
    sum_c = [0 for i in range(n + 1)]
    for i in range(n):
        r = list(map(int, input().split()))
        r.append(sum(r))
        ans.append(r)
        for j in range(n + 1):
            sum_c[j] += r[j]
    ans.append(sum_c)
    for i in range(n + 1):
        [print("{:>5}".format(str(ans[i][j])),end="") for j in range(n + 1)]
        print()

Solution (Python):
while True:
    n = int(input())
    if n == 0: break
    table = []
    for _ in range(n):
        num = list(map(int, input().split()))
        num.append(sum(num))
        table.append(num)
    vertical = []
    for t in zip(*table):
        vertical.append(sum(t))
    table.append(vertical)
    for tab in table:
        for t in tab:
            print('{:>5d}'.format(t), end='')
        print()

Solution (Python):
while True:
    n = int(input())
    if n == 0:
        break
    mat = []
    for i in range(n):
        mat.append(list(map(int, input().split())))
        mat[i].append(sum(mat[i]))
    mat.append([0]*(n+1))
    for i in range(n+1):
        for j in range(n):
            mat[n][i]+=mat[j][i]
    for i in mat:
        for j in range(n+1):
            print("%5d" % (i[j]), end="")
        print()

Solution (Python):
n = int(input())
while (n != 0):
    nlist = []
    for i in range(n):
        nn = list(map(int, input().split()))
        nn.append(sum(nn))
        nlist.append(nn)
    lastn = []
    for i in range(n+1):
        last = 0
        for k in range(n):
            last += nlist[k][i]
        lastn.append(last)
    nlist.append(lastn)
    for i in range(n+1):
        for k in range(n+1):
            nnn = nlist[i][k]
            l = len(str(nnn))
            cn = 5 - l
            if (k == n):
                print(' '*cn + '{}'.format(nnn))
            else:
                print(' '*cn + '{}'.format(nnn), end = '')
    
    n = int(input())

Solution (Python):
while(True):
    n = int(input())
    if n == 0:
         break
    arr = []
    for i in range(n):
        arr.append(list(map(int, input().split())))
    arr = [ l+[sum(l)] for l in arr]
    brr = [0]*(n+1)
    for i in range(n):
        for j in range(n+1):
            brr[j] += arr[i][j]
    crr = arr+[brr]
    for c in crr:
        for d in c:
            print("%5d"%d,end="")
        print()