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 True:
    n = int(input())
    if n == 0: break
    table = []
    for _ in range(n):
        num = list(map(int, input().split()))
        num.append(sum(num))
        table.append(num)
    vertical = []
    for t in zip(*table):
        vertical.append(sum(t))
    table.append(vertical)
    for tab in table:
        for t in tab:
            print('{:>5d}'.format(t), 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))

    for i in range(row):
        for j in range(row+1):
            print(str(tab[i][j])+" ", end="")
        print()
        
    for i in range(row+1):
        print(str(bel[i])+" ", end="")
    print()

Python: 
while True:
    L = []
    n = int(input())
    if n == 0:
        break
    for _ in range(n):
        l = [int(x) for x in input().split()]
        l.append(sum(l))
        L.append(l)

    l = []
    for i in range(len(L[0])):
        s = []
        for j in range(len(L)):
            s.append(L[j][i])
        l.append(sum(s))
    L.append(l)

    for l in L:
        for i in l:
            print("{:>5d}".format(i),end="")
        print()

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

Python: # coding=utf-8

###
### for python program
###

import sys
import math

# math class
class mymath:
    ### pi
    pi = 3.14159265358979323846264338

    ### Prime Number
    def pnum_eratosthenes(self, n):
        ptable = [0 for i in range(n+1)]
        plist = []

        for i in range(2, n+1):
            if ptable[i]==0:
                plist.append(i)
                for j in range(i+i, n+1, i):
                    ptable[j] = 1
        return plist

    ### GCD
    def gcd(self, a, b):
        if b == 0:
            return a
        return self.gcd(b, a%b)

    ### LCM
    def lcm(self, a, b):
        return (a*b)//self.gcd(a,b)

    ### Mat Multiplication
    def mul(self, A, B):
        ans = []
        for a in A:
            c = 0
            for j, row in enumerate(a):
                c += row*B[j]
            ans.append(c)
        return ans

mymath = mymath()

### output class
class output:
    ### list
    def list(self, l):
        l = list(l)
        #print(" ", end="")
        for i, num in enumerate(l):
            print(num, end="")
            if i != len(l)-1:
                print(" ", end="")
        print()

output = output()

### input sample
#i = input()
#N = int(input())
#A, B, C = [x for x in input().split()]
#N, K = [int(x) for x in input().split()]
#inlist = [int(w) for w in input().split()]

#R = float(input())
#A.append(list(map(int,input().split())))

#for line in sys.stdin.readlines():
#    x, y = [int(temp) for temp in line.split()]

#abc list
#abc = [chr(ord('a') + i) for i in range(26)]

### output sample
# print("{0} {1} {2:.5f}".format(A//B, A%B, A/B))
# print("{0:.6f} {1:.6f}".format(R*R*math.pi,R*2*math.pi))
# print(" {}".format(i), end="")

# def printA(A):
#     for i, n in enumerate(A):
#         print(n, end='')
#         if i != N-1:
#             print(' ', end='')
#     print()

# リスト内包表記 ifあり
# [x-k if x != 0 else x for x in C]

# ソート
# N = N.sort()

# 10000個の素数リスト
# P = mymath.pnum_eratosthenes(105000)

def get_input():
    N = []
    while True:
        try:
            N.append(input())
            #N.append(int(input()))
            #N.append(float(input()))
        except EOFError:
            break
    return N

def is_integer(n):
    try:
        float(n)
    except ValueError:
        return False
    else:
        return float(n).is_integer()

def dist(A, B):
    d = 0
    for i in range(len(A)):
        d += (A[i]-B[i])**2            
    d = d**(1/2)
    return d

while True:
    N = int(input())
    A = []

    if N == 0:
        break

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

    B = []

    for i in range(N+1):
        temp = 0
        for j in range(N):
            temp += A[j][i]
        B.append(temp)
    A.append(B)

    for row in A:
        for i in row:
            s = str(i)
            print(s.rjust(5, ' '), end='')
        print()
        pass

Python: while(1):
    n = int(input())
    if n == 0:
        break
    t = [[0] * (n + 1) for i in range(n + 1)]
    m = []
    for i in range(0,n):
        m.append(list(int(x) for x in input().split(" ")))
    sum = 0
    for i in range(0,n):
        sum1 = 0
        sum2 = 0
        for j in range(0,n):
            t[i][j] = m[i][j]
            sum1 += m[i][j]
            sum2 += m[j][i]
        t[i][n] = sum1
        t[n][i] = sum2
        sum += sum1
    t[n][n] = sum
    
    
    for i in range(0,n + 1):
        for j in range(0,n + 1):
            print("%5d" %(t[i][j]),end="")
        print()

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

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

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) < _ + 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), end='')