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

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

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

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

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

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]
	print(a)
	print(t)

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

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

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

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)
				sys.stdout.write(' ')
			else:
				print('{:>5}'.format(data[i][j]))

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

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

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