Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	stdin := bufio.NewScanner(os.Stdin)
	for stdin.Scan() {
		n, _ := strconv.Atoi(stdin.Text())
		if n == 0 {
			break
		}

		a := make([][]int, n+1)
		for i := 0; i < n; i++ {
			sum := 0
			stdin.Scan()
			a[i] = make([]int, n+1)
			str := strings.Split(stdin.Text(), " ")
			for j := 0; j < n; j++ {
				a[i][j], _ = strconv.Atoi(str[j])
				sum += a[i][j]
				fmt.Printf("%5d", a[i][j])
			}
			a[i][n] = sum
			fmt.Printf("%5d\n", a[i][n])
		}
		a[n] = make([]int, n+1)
		for i := 0; i <= n; i++ {
			sum := 0
			for j := 0; j < n; j++ {
				sum += a[j][i]
			}
			a[n][i] = sum
			fmt.Printf("%5d", a[n][i])
		}
		fmt.Println()
	}
}

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

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

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='')

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

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

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

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)

Python: #!/usr/bin/python3

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

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

Python: data=[]
while True:
    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('')

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

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

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