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:
    inputCount = int(input())

    if inputCount == 0:
        break

    table = []

    for lp in range(inputCount):
        content = [int(item) for item in input().split(" ")]
        content.append(sum(content))
        table.append(content)

    table.append([])

    for col in range(inputCount + 1):
        total = 0
        for row in range(inputCount):
            total += table[row][col]

        table[inputCount].append(total)

    output = []
    for array in table:
        content = ["{:>5}".format(str(item)) for item in array]
        output.append(str(content))

    print("\n".join(output))

Python: def output_res(row):
    print(''.join(map(lambda num: str(num).rjust(5), row)), str(sum(row)).rjust(5), sep='')


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

        bottom_total = [0 for _ in range(n)]
        for _ in range(n):
            each_line = list(map(int, input().split(' ')))
            output_res(each_line)
            bottom_total = [x + y for x, y in zip(bottom_total, each_line)]

        output_res(bottom_total)


if __name__ == '__main__':
    main()

Python: # AOJ 0102: Matrix-like Computation
# Python3 2018.6.17 bal4u

while True:
	n = int(input())
	if n == 0: break
	arr = [[0 for r in range(n+2)] for c in range(n+2)]

	for r in range(n):
		arr[r] = list(map(int, input().split()))
		arr[r].append(sum(arr[r]))

	for c in range(n+1):
		s = 0
		for r in range(n): s += arr[r][c]
		arr[n][c] = s

	for r in range(n+1):
		for c in range(n+1): print(format(arr[r][c], '5d'), end='')
		print()

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