Go: package main

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

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

		input := make([][]int, n+1, n+1)
		totalRow := make([]int, n+1, n+1)

		for i := 0; i < n; i++ {
			row := make([]int, n+1, n+1)
			scanner.Scan()

			row = numbers(scanner.Text())

			input[i] = row
			total(totalRow, row)
		}

		input[n] = totalRow

		for xk := range input {
			for yk, yv := range input[xk] {
				if yk == n {
					fmt.Printf("%5d\n", yv)
				} else {
					fmt.Printf("%5d", yv)
				}
			}
		}
	}
}

func total(sum, row []int) {
	for i := 0; i < len(row); i++ {
		sum[i] += row[i]
	}
}

func numbers(s string) []int {
	var n []int
	var sum int
	for _, f := range strings.Fields(s) {
		i, err := strconv.Atoi(f)
		if err == nil {
			n = append(n, i)
			sum += i
		}
	}
	n = append(n, sum)
	return n
}

Python: from sys import stdin

if __name__ == '__main__':
    # is_begin = False
    for n in stdin:
        if int(n) == 0:
            break
        # else:
        #     if is_begin:
        #         print()
        # is_begin = True

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

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

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

        for i in range(n):
            num = 0
            m = map(int, input().split())
            for j in m:
                print("{:5}".format(j), end='')
                num += j
            print("{:5}".format(num))

if __name__ == '__main__':
    main()

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

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