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

Python: while 1:
 n=int(input())
 if n==0:break;
 a=[]
 for i in range(n):
  a[i]=[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 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}")

Python: data=[]
while True:
    try:
        n = int(input())
    except:
        break
    if n==0:
        break
    for i in range(n):
        spam=list(map(int,input().split()))
        spam.append(sum(spam))
        data.append(spam)
        for j in spam:
            print(str(j)+' ',end='')
        print('')
    for i in range(len(data)+1):
        print(str(sum(x[i] for x in data))+' ',end='')
    print('')

Python: if __name__ == "__main__":
    while True:
        flag = input()
        if flag == 0:
            break
        num, ans = [], [0]*(flag + 1)
        for i in xrange(flag):
            num.append(map(int, raw_input().split()))
        for i in xrange(len(num)):
            for j in xrange(len(num[i])):
                print ("     " + str(num[i][j]))[-4:],
                ans[j] += num[i][j]
            print ("     " + str(sum(num[i])))[-4:]
            ans[j+1] += sum(num[i])

        for i in xrange(flag + 1):
            if i == flag:
                print ("     " + str(ans[i]))[-4:]
            else:
                print ("     " + str(ans[i]))[-4:],

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: 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) < _ + 1:
                    bottom_total.append(int(v))
                else:
                    bottom_total[_] += 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(right_total)).rjust(5))

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(str, l))) 
    print(' '.join(map(str, bottom_record)))
    n = int(input())

Python: 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((" ").join(list(map(str,list_row))))
        print((" ").join(list(map(str,sum_col))))
    else:
        break

Python: n=int(input())
def sum1(x,y):
    num=0
    for i in range(n):
        num+=x[i][y]
    return int(num)
            
Matrix=[]
for i in range(n+1):
    Matrix.append(i)
    if i==n:
        Matrix[i]=[]
        for j in range(5):
            Matrix[i].append(sum1(Matrix,j))
    else:
        Matrix[i]=[int(k) for k in input().split()]
        Matrix[i].append(sum(Matrix[i]))

for i in range(n+1):
    for j in range(5):
        print(Matrix[i][j],end=" ")
    print("")

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])]
        print(*a[i])
    a=list(zip(*a[::-1]))
    for i in range(n):print(sum(a[i]),end=' ')
    print(sum(a[n]))

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

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)

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