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):
while 1:
  n=input()+1
  if n==1: break
  x=[]
  y=[0]*n
  N=range(n)
  for i in N[:-1]:
    a=map(int,raw_input().split(' '))
    a.append(sum(a))
    x.append(a)
    for j in N: y[j]+=a[j]
  x.append(y)
  for i in N: print "".join([str(e).rjust(5) for e in x[i]])

Solution (Python):
while 1:
  n=input()+1
  if n==1: break
  x=[]
  y=[0]*n
  for _ in range(n-1):
    a=map(int,raw_input().split(' '))
    a.append(sum(a))
    s=""
    for i in range(n):
      y[i]+=a[i]
      s+=str(a[i]).rjust(5)
    print s
  print "".join([str(e).rjust(5) for e in y])

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

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

Solution (Python):
while True:
	n=int(input())
	if not n: break
	a=[list(map(int,input().split())) for _ in range(n)]
	t=[[0 for _ in range(n+1)] for _ in range(n+1)]
	for i in range(n):
		for j in range(n):
			t[i][j]=a[i][j]
			t[i][n]+=a[i][j]
			t[n][j]+=a[i][j]
			t[n][n]+=a[i][j]
	for i in range(n+1):
		print("".join(map("{0:>5}".format,t[i])))

Solution (Python):


while True:
    n = int(input())
    if (n == 0):
        break
    mat = [ [] for _ in range(n) ]
    row = [ 0 for _ in range(n) ]
    col = [ 0 for _ in range(n) ]
    for i in range(n):
        ln = list(map(int, input().split(' ')))
        mat[i] = ln
        row[i] = sum(ln)
        for j in range(n):
            col[j] += ln[j]

    total = sum(col)

    for i in range(n):
        ls = mat[i]
        ls.append(row[i])
        print(''.join(['%5d' % x for x in ls]))

    col.append(sum(col))
    print(''.join(['%5d' % x for x in col]))

Solution (Python):



def main():
    while(True):
        
        N = int(raw_input())
        if N == 0:
            break

        inp = []
        for x in range(N):
            input_list = map(int, raw_input().split())
            inp.append(input_list)

        printAns(inp)

def printAns(num_list):
    add_list = []
    for x in num_list:
        x.append(sum(x))
        add_list.append(x)

    total_list = [sum(x) for x in zip(*add_list)]
    add_list.append(total_list)
    print5digit(add_list)

def print5digit(num_list):
    for x in num_list:
        print "".join(map(lambda x_: str(x_).rjust(5), x))
        
        
        
        

if __name__=='__main__':
    main()

Solution (Python):



def main():
    while(True):
        
        N = int(raw_input())
        if N == 0:
            break

        inp = []
        for x in range(N):
            input_list = map(int, raw_input().split())
            inp.append(input_list)

        printAns(inp)

def printAns(num_list):
    add_list = []
    for x in num_list:
        x.append(sum(x))
        add_list.append(x)

    total_list = [sum(x) for x in zip(*add_list)]
    add_list.append(total_list)
    print5digit(add_list)

def print5digit(num_list):
    for x in num_list:
        print "".join(map(lambda x_: str(x_).rjust(5), x))

if __name__=='__main__':
    main()

Solution (Python):
while True:
    n = int(raw_input())
    if n == 0:
        break
    else: 
        table = []
        for m in range(n):
            line = []
            
            line = map(int,raw_input().split())

            line_sum = sum(line)
            line.append(line_sum)
            table.append(line)
            
        
        
        vert_sum_line=[]
        for k in range(n):
            vert_sum = 0
            for j in range(n):
                vert_sum += table[j][k]
            vert_sum_line.append(vert_sum)
        
        total = sum(vert_sum_line)
        vert_sum_line.append(total)
    
        
        temp = []
        for line in table:
            temp.append( [str(x).rjust(5) for x in line])
        temp.append([str(x).rjust(5) for x in vert_sum_line])
        
        for i in xrange(n+1):
            print "".join(temp[i])

Solution (Python):
while True:
    data=[]
    n = int(input())
    if n==0:
        break
    for i in range(n):
        spam=list(map(int,input().split()))
        spam.append(sum(spam))
        data.append(spam)
    spam=[]
    for i in range(n+1):
        spam.append(sum(x[i] for x in data))
    data.append(spam)
    for i in range(n+1):
        for j in range(n+1):
            print('{:5}'.format(data[i][j]),end='')
        print('')

Solution (Python):
while True:
	n = int(raw_input())
	if n == 0:
		break
	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])

Solution (Python):
while(1):
    n=int(raw_input())
    if n==0:
        break
    else:
        F=[]
        for i in range(n):
            F.append([int(x) for x in raw_input().split()])
        F.append([0 for x in range(n+1)])
        for i in range(n):
            F[i]=F[i]+[sum(F[i])]
        for j in range(n+1):
            s=0
            for i in range(n):
                s=s+F[i][j]
            F[n][j]=s
        G=[[' '*(5-len(str(x)))+str(x) for x in y] for y in F]
        for i in range(n+1):
            print ''.join(G[i])

Solution (Python):
while True:
    num = input()
    if num == 0: break

    mrx = []
    row = [0] * (num + 1)
    for i in range(num):
        mrx.append(map(int, raw_input().split()))
        mrx[i].append(sum(mrx[i]))
        for j in range(num + 1): row[j] += mrx[i][j]
    mrx.append(row)
    for i in mrx: print "".join([str(s).rjust(5) for s in i])

Solution (Python):
while True:
    data=[]
    n=int(input())
    if n==0:break
    for i in range(n):
        a=list(map(int,input().split(' ')))
        a.append(sum(a))
        data.append(a)
    a=[]
    for i in range(n+1):
        a.append(sum(x[i] for x in data))
    data.append(a)
    for i in range(n+1):
        for j in range(n+1):
            print('{:5}'.format(data[i][j]),end='')
        print('')

Solution (Python):
while True:
    N = int(raw_input())
    if N == 0: break
    total = None
    result = []
    for i in range(N):
        lis = list(map(int, raw_input().split(' ')))
        lis.append(sum(lis))
        result.append(lis)
        if total == None:
            total = lis[:]
        else:
            for i in range(len(total)):
                total[i] = total[i]+lis[i]
    else:
        result.append(total)
        for line in result:
            ans = []
            for v in line:
                ans.append('%05s' % v)
            else:
                print ''.join(ans)

Solution (Python):
def print_row(iterable):
    print(''.join(map(lambda x: str(x).rjust(5), iterable)),
          str(sum(iterable)).rjust(5), sep='')


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

    m = []
    for i in range(n):
        m.append(list(map(int, input().split())))

    last = [0 for i in range(n)]
    for row in m:
        print_row(row)
        for (i, e) in enumerate(row):
            last[i] += e

    print_row(last)

Solution (Python):
import sys
try:
	while 1:
		temp = input()
		if (temp==0):
			break
		N = temp
		a = []
		for i in range(N):
			a.append(map(int, raw_input().split())+[0])
		a.append([0 for i in range(0,temp+1)])
		for i in range(0,temp):
			for j in range(0,temp):
				a[i][temp] += a[i][j]
		for k in range(0,temp+1):
			for l in range(0,temp):
				a[temp][k] += a[l][k]
		for m in range(0,temp+1):
			for n in range(0,temp+1):
				sys.stdout.write(str(a[m][n]).rjust(5))
			print ""
except(RuntimeError):
	pass

Solution (Python):
while True:
  l = []
  n = int(input())
  if n == 0:
    break
  lx = [0 for i in range(n+1)]
  for j in range(n):
    l1 = [int(i) for i in input().split()]
    c = 0
    for i in l1:
      c += i
    l1.append(c)
    for i in range(n+1):
      lx[i] += l1[i]
    l.append(l1)
  l.append(lx)
  for s in l:
    a = ''
    for t in s:
      a += ' ' * (5-len(str(t)))
      a += str(t)
    print(a)

Solution (Python):
n = int(raw_input())
while n != 0:
    input_list = []
    for x in range(n):
        input_list.append(map(int, raw_input().split(' ')))
    for x in range(n):
        input_list[x].append(sum(input_list[x]))
        print ''.join(map(lambda n:"{:>5}".format(n), input_list[x]))

    input_len = len(input_list[0])
    column_list = []
    for x in range(input_len):
        tmp = 0
        for y in range(n):
            tmp += input_list[y][x]
        column_list.append(tmp)
    print ''.join(map(lambda n:"{:>5}".format(n), column_list))
    n = int(raw_input())

Solution (Python):
from sys import stdin

if __name__ == '__main__':
    
    for n in stdin:
        if int(n) == 0:
            break
        
        
        
        

        bottom_total = []
        right_total = []
        for _ in range(int(n)):
            line = input().split(' ')
            total = 0
            for i, v in enumerate(line):
                print(repr(int(v)).rjust(5), end='')
                total += int(v)
                if len(bottom_total) < i + 1:
                    bottom_total.append(int(v))
                else:
                    bottom_total[i] += int(v)
            right_total.append(total)
            print(repr(right_total[_]).rjust(5))

        for num in bottom_total:
            print(repr(num).rjust(5), end='')
        print(repr(sum(bottom_total)).rjust(5))

Solution (Python):
def output_res(row):
    print(''.join(map(lambda num: str(num).rjust(5), row)), str(sum(row)).rjust(5), sep='')


def main():
    while True:
        n = int(input())
        if n == 0:
            break

        bottom_total = [0 for _ in range(n)]
        for _ in range(n):
            each_line = list(map(int, input().split(' ')))
            output_res(each_line)
            bottom_total = [x + y for x, y in zip(bottom_total, each_line)]

        output_res(bottom_total)


if __name__ == '__main__':
    main()

Solution (Python):
def print_list_inline(tr_list):
    inline = map(lambda string:"{:>5}".format(string),tr_list)
    inline = list(inline)
    print(("").join(inline))


while True:
    n = int(input())
    if n != 0:
        sum_col = [0 for i in range(n+1)]
        for i in range(n):
            list_row = list(map(int,input().split(" ")))
            sum_row = sum(list_row)
            list_row.append(sum_row)
            for j in range(len(list_row)):
                sum_col[j] += list_row[j]
            print_list_inline(list_row)
        print_list_inline(sum_col)
    else:
        break

Solution (Python):
while True:
    n = int(input())
    if n == 0:
        break
    a = []
    b = []
    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]))
    for i in range(n + 1):
        sum_ = 0
        for j in range(n):
            sum_ += a[j][i]
        b.append(sum_)
    a.append(b)

    for i in range(n + 1):
        for j in range(n + 1):
            if j == n:
                print('{:>5}'.format(a[i][j]))
            else:
                print('{:>5}'.format(a[i][j]), end='')

Solution (Python):
while 1:
    n=int(raw_input())
    if n==0:break
    a=[map(int,raw_input().split()) for _ in xrange(n)]
    ans=[[0]*(n+1) for _ in xrange(n+1)]
    for i in xrange(n+1):
        for j in xrange(n+1):
            if i==n :
                if j==n:
                    ans[n][n]+=sum(ans[n])
                else:
                    for k in xrange(n):
                        ans[n][j]+=a[k][j]
            else:
                if j==n:
                    for k in xrange(n):
                        ans[i][n]+=a[i][k]
                else:
                    ans[i][j]=a[i][j]
    for i in xrange(n+1):
        print("".join(map(lambda x:str(x).rjust(5),ans[i])))

Solution (Python):
while True:
	n = int(input())
	if n == 0:
		break
	a = [list(map(int, input().split())) for i in range(n)]
	t = [[0 for i in range(n + 1)] for i in range(n + 1)]
	for i in range(n):
		for j in range(n):
			t[i][j] = a[i][j]
			t[i][n] += a[i][j]
			t[n][j] += a[i][j]
			t[n][n] += a[i][j]
	for i in range(n + 1):
		print("".join(map("{0:>5}".format,t[i])))

Solution (Python):
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('{:>5}'.format(str(tab[i][j])), end='')
        print()

Solution (Python):
while True:
    Num_lis =[]
    n = int(input())
    if n == 0:
        break
    for i in range(n):
        Num_lis.append(list(map(int,input().split())))

    for j in range(n):
        sum_num = 0
        for k in range(n):
            sum_num += Num_lis[j][k]
        Num_lis[j].append(sum_num)

    Num_lis.append([])
    for l in range(n + 1):
        wa = 0
        for m in range(n):
            wa += Num_lis[m][l]
        Num_lis[n].append(wa)
    for o in range(n + 1):
        s_num = ""
        for p in range(n + 1):
            s_num = s_num + '{:>5}'.format(Num_lis[o][p])
        print(s_num)

Solution (Python):
def rowSum(matrix):
  n=len(matrix)
  sumcolumn=[0]*n
  for i in range(0,n):
    sumcolumn[i] = sum(matrix[i])
  return sumcolumn

def colSum(matrix):
  n=len(matrix)
  sumrow=[0]*n
  for i in range(0,n):
    for j in range(0,n):
      sumrow[i]+=matrix[j][i]
  return sumrow

while True:
  n=int(input())
  if n==0:  break
  matrix=[None]*n
  for i in range(0,n):
    matrix[i] = list(map(int,input().split()))

  c=rowSum(matrix)
  r=colSum(matrix)

  matrix.append(r)

  for i in range(0,n):
    matrix[i].append(c[i])

  matrix[n].append(sum(matrix[n]))

  for row in matrix:
    line=""
    for element in row:
      line += '{:>5}'.format(str(element))
    line.rstrip()
    print(line)

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

Solution (Python):
while True:
    n = int(input())
    if n == 0:
        break
    arr = [[0] * n] * n
    for i in range(n):
        arr[i] = list(map(int,input().split()))

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

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

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

    for i in range(len(arr[i])-1):
        for j in range(len(arr[i])):
            print('{0:5d}'.format(arr[i][j]),end = '')
        print('')
    for i in range(len(last_line)):
        print('{0:5d}'.format(last_line[i]),end = '')
    print('')

Solution (Python):
from copy import deepcopy

def main():
    while True:
        n = int(input())
        if n == 0:
            break

        tate = [0 for i in range(n)]

        for i in range(n):
            num = 0
            m = map(int, input().split())
            hoge = deepcopy(m)

            tate = [x + y for (x, y) in zip(tate, hoge)]

            for j in m:
                print("{:5}".format(j), end='')
                num += j
            print("{:5}".format(num))

        num = 0
        for j in tate:
            print("{:5}".format(j), end='')
            num += j
        print("{:5}".format(num))

if __name__ == '__main__':
    main()

Solution (Python):
import functools

n = int(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(input())

Solution (Python):
n = int(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(input())

Solution (Python):
def main():
    while True:
        N = int(input())
        if N == 0:
            break

        A = [
            list(map(int, input().split(" ")))
            for _ in range(N)
        ]
        tot = [sum(col[r] for col in A) for r in range(N)]
        A.append(tot)
        for col in A:
            col.append(sum(col))

        for col in A:
            print("".join("{:5}".format(x) for x in col))


if __name__ == "__main__":
    main()

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

Solution (Python):


import sys
import os

for s in sys.stdin:
    n = int(s)
    if n == 0:
        break

    M = []
    for i in range(n):
        lst = list(map(int, input().split()))
        lst.append(sum(lst))
        M.append(lst)

    last_lst = []
    
    for i in range(n+1):
        sumv = 0
        for row in M:
            sumv += row[i]
        last_lst.append(sumv)
    M.append(last_lst)

    
    for row in M:
        for val in row:
            print(str(val).rjust(5), end='')
        print()

Solution (Python):
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")

Solution (Python):
while True:
    n = int(input())
    if n == 0:
        break
    rows = [list(map(int, input().split())) for _ in range(n)]
    for row in rows:
        print("".join(map("{0:>5}".format, row)), end="")
        print("{:>5}".format(sum(row)))
    columns = list(zip(*rows))
    print("".join(map("{0:>5}".format, list(sum(column) for column in columns))), end="")
    print("{0:>5}".format(sum(sum(row) for row in rows)))

Solution (Python):
while(1):
	box = []
	num = int(input())
	if num == 0: break
	[box.append(list(map(int,input().split()))) for i in range(num)]
	[box[i].append(sum(box[i])) for i in range(num)]
	box_replace = list(map(list, zip(*box)))
	temp = [[]]
	[temp[0].append(sum(box_replace[i])) for i in range(num+1)]
	box += temp
	box = [list(map(str, box[i])) for i in range(num+1)]
	box = ([[ box[i][j].rjust(5," ") for j in range(num+1)] for i in range(num+1)] )
	
	[print("".join(box[i])) for i in range(num+1)]

Solution (Python):

"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0102&lang=jp
"""
import sys


def solve(data):
    for d in data:
        d.append(sum(d))

    v_sum = [0] * len(data[0])
    for d in data:
        for x, n in enumerate(d):
            v_sum[x] += n
    data.append(v_sum)
    return data


def main(args):
    
    
    
    
    
    while True:
        num = int(input())
        if num == 0:
            break
        data = []
        for _ in range(num):
            data.append([int(x) for x in input().split(' ')])
        result = solve(data)
        for row in result:
            for n in row:
                print('{:5d}'.format(n), end='')
            print('')


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
import sys
def main():
    for line in sys.stdin:
        n = int(line)
        if n != 0:
            a = []
            for _ in range(n):
                a.append(list(map(int, input().split())))
            a.append(list(range(n + 1)))

            for x in range(n):
                hoge = 0
                for y in range(n):
                    hoge += a[y][x]
                else:
                    a[n][x] = hoge

            for x in range(n + 1):
                hoge = 0
                for y in range(n):
                    hoge += a[x][y]
                else:
                    if x != n:
                        a[x].append(hoge)
                    else:
                        a[n][n] = hoge

            for x in range(n + 1):
                for y in range(n + 1):
                    print("{0:5d}".format(a[x][y]), end = "")
                else:
                    print()

        else:
            break


if __name__ == "__main__":
    main()

Solution (Python):


import sys, math, os


PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


while True:
    N = int(input())
    if N == 0:
        break
    M = [[int(_) for _ in input().split()] for __ in range(N)]
    for i in range(N):
        M[i].append(sum(M[i]))
    last = [sum([M[row][col] for row in range(N)]) for col in range(N + 1)]
    M.append(last)
    fmt = "%5d" * (N + 1)
    for row in M:
        print(fmt % tuple(row))

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

    a = []
    for i in range(n):
        a.append([int(num) for num in input().split()])

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


    a.append([])
    for i in range(n + 1):
        sum = 0
        for j in range(n):
            sum += a[j][i]
        a[n].append(sum)

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

Solution (Python):
import sys
import math

while True:
	n = int(input())
	if n == 0:
		break
	data = [list(map(int,input().split())) for i in range(n)]
	data.append([0 for i in range(n+1)])

	for i in range(n+1):
		if i != n:
			data[i].append(sum(data[i]))
		for j in range(n):
			data[n][i] = data[n][i]+data[j][i]
	for i in range(n+1):
		for j in range(n+1):
			if j != n:
				string = str('{:>5}'.format(data[i][j]))
				sys.stdout.write(string)
			else:
				print('{:>5}'.format(data[i][j]))

Solution (Python):
import sys


def print2d(m):
    for r in m:
        print("".join(["{:5d}".format(x) for x in r]))


def solve():
    while True:
        n = int(sys.stdin.readline())
        if n == 0 :
            return
        m = []
        for i in range(n):
            m.append([int(x) for x in sys.stdin.readline().split(' ')])
        for r in range(n):
            m[r].append(sum(m[r]))
        lastRow = []
        for c in range(n+1):
            s = 0
            for r in range(n):
                s += m[r][c]
            lastRow.append(s)
        m.append(lastRow)
        print2d(m)
solve()

Solution (Python):
while True:
    n = int(input())

    if n == 0:
        break

    matrix = [[] for _ in range(n)]
    col_sum = [0 for _ in range(n + 1)]
    for i in range(n):
        matrix[i] = list(map(int, input().split()))
        matrix[i].append(sum(matrix[i]))
        for j in range(n + 1):
            col_sum[j] += matrix[i][j]

    matrix.append(col_sum)

    for i in range(n + 1):
        for j in range(n + 1):
            print('%5d' % matrix[i][j], end='')
        print()

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:
    inputCount = int(input())

    if inputCount == 0:
        break

    table = []

    for lp in range(inputCount):
        content = [int(item) for item in input().split(" ")]
        content.append(sum(content))
        table.append(content)

    table.append([])

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

        table[inputCount].append(total)

    for array in table:
        print("".join("{:>5}".format(item) for item in array))

Solution (Python):
while 1:
 n=int(input())
 if n==0:break;
 a=[list(map(int,input().split()))for _ in[0]*n]
 for r in a:
  for s in r:print(f"{s:5}",end='')
  print(f"{sum(r):5}")
 for c in zip(*a):print(f"{sum(c):5}",end='')
 print(f"{sum([sum(r)for r in a]):5}")

Solution (Python):
while 1:
 n=int(input())
 if n==0:break;
 a=[list(map(int,input().split()))for _ in[0]*n]
 for r in a:
  for s in r:print(f"{s:5}",end='')
  print(f"{sum(r):5}")
 for c in zip(*a):print(f"{sum(c):5}",end='')
 print(f"{sum([sum(r)for r in a]):5}")

Solution (Python):
while 1:
 n=int(input())
 if n==0:break;
 a=[list(map(int,input().split()))for _ in[0]*n]
 for r in a:
  for s in r:print(f"{s:5}",end='')
  print(f"{sum(r):5}")
 for c in zip(*a):print(f"{sum(c):5}",end='')
 print(f"{sum([sum(r)for r in a]):5}")

Solution (Python):
while 1:
 n=int(input())
 if n==0:break;
 a=[]
 for _ in[0]*n:
  b=list(map(int,input().split()))
  b+=[sum(b)]
  a+=[b]
  for s in b:print("%5d"%s,end='')
  print()
 for c in zip(*a):print("%5d"%sum(c),end='')
 print()

Solution (Python):
while 1:
 n=int(input())
 if n==0:break;
 a=[]
 for _ in[0]*n:
  b=list(map(int,input().split()))
  b+=[sum(b)]
  a+=[b]
  for s in b:print("%5d"%s,end='')
  print()
 for c in zip(*a):print("%5d"%sum(c),end='')
 print()

Solution (Python):
while 1:
 n=int(input())
 if n==0:break;
 a=[]
 for _ in[0]*n:
  b=list(map(int,input().split()))
  b+=[sum(b)]
  a+=[b]
  for s in b:print("%5d"%s,end='')
  print()
 for c in zip(*a):print("%5d"%sum(c),end='')
 print()

Solution (Python):
while 1:
 n=int(input())
 if n==0:break;
 a=[]
 for _ in[0]*n:
  b=list(map(int,input().split()))
  b+=[sum(b)]
  a+=[b]
  for s in b:print("%5d"%s,end='')
  print()
 for c in zip(*a):print("%5d"%sum(c),end='')
 print()

Solution (Python):
while 1:
 n=int(input())
 if n==0:break;
 a=[]
 for _ in[0]*n:
  b=list(map(int,input().split()))
  b+=[sum(b)]
  a+=[b]
  for s in b:print("%5d"%s,end='')
  print()
 for c in zip(*a):print("%5d"%sum(c),end='')
 print()

Solution (Python):
while 1:
 n=int(input())
 if n==0:break;
 a=[]
 for i in range(n):
  a+=[list(map(int,input().split()))]
  a[i]+=[sum(a[i])]
  for s in a[i]:print("%5d"%s,end='')
  print()
 for c in zip(*a):print("%5d"%sum(c),end='')
 print()

Solution (Python):
while 1:
 n=int(input())
 if n==0:break;
 a=[]
 for i in range(n):
  a+=[list(map(int,input().split()))]
  a[i]+=[sum(a[i])]
  for s in a[i]:print("%5d"%s,end='')
  print()
 for c in zip(*a):print("%5d"%sum(c),end='')
 print()

Solution (Python):
while 1:
 n=int(input())
 if n==0:break;
 a=[]
 for i in range(n):
  a+=[list(map(int,input().split()))]
  a[i]+=[sum(a[i])]
  for s in a[i]:print("%5d"%s,end='')
  print()
 for c in zip(*a):print("%5d"%sum(c),end='')
 print()

Solution (Python):
while 1:
 n=int(input())
 if n==0:break;
 a=[]
 for i in range(n):
  b=list(map(int,input().split()))
  a+=[b+[sum(b)]]
  for s in a[i]:print("%5d"%s,end='')
  print()
 for c in zip(*a):print("%5d"%sum(c),end='')
 print()

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

    table = [[0 for i in range(N+1)] for j in range(N+1)]

    for i in range(N):
        n = [int(i) for i in input().split()]
        for j in range(N):
            table[i][j] = n[j]

    for i in range(N):
        for j in range(N):
            table[i][N] += table[i][j]
            table[N][j] += table[i][j]

    for i in range(N):
        table[N][N] += table[i][N]

    for i in range(N+1):
        for j in range(N+1):
            print(str(table[i][j]).rjust(5), end="")
        print("")

Solution (Python):
while True:
    rows = []
    total = {}
    n = int(input())
    if (n == 0):
        break

    for r in range(n):
        datas = map(int, input().split())
        ds = list(datas)
        rows.append(ds)
            
    for r in range(n):
        s = 0
        for c in range(n):
            s += rows[r][c]
            if (c not in total):
                total[c] = rows[r][c]
            else:
                total[c] += rows[r][c]

        rows[r].append(s)
        if (n not in total):
            total[n] = s
        else:
            total[n] += s

    for r in range(n):
        for c in range(len(rows[r])):
            print("{0:>5}".format(rows[r][c]), end="")
        print()

    for t in range(len(total)):
        print("{0:>5}".format(total[t]), end="")
    print()

Solution (Python):
while True:
  n = int(input())
  if not n:
    break

  mat = []
  for _ in range(n):
    lst = list(map(int, input().split()))
    lst.append(sum(lst))
    mat.append(lst)
  
  sum_lst = []
  for i in range(len(mat[0])):
    s = 0
    for j in range(n):
      s += mat[j][i]
    sum_lst.append(s)
  mat.append(sum_lst)

  for line in mat:
    put = ""
    for i in line:
      put += str(i).rjust(5)
    print(put)

Solution (Python):
while 1:
  l=[list(map(int,input().split())) for i in range(int(input()))]
  if not l:break
  le=len(l)
  for (i,j) in enumerate(l):
    l[i].append(sum(j))
  l.append([sum([l[k][i] for k in range(le)]) for i in range(le+1)])
  [print("".join([str(j).rjust(5) for j in i])) for i in l]

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()