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

Python: while 1:
    try:
        N=int(input())
        if N==0:break
        matrix = [list(map(int,input().split())) for i in range(N)]
        matrix.append([])
        for i in range(N):matrix[i].append(sum(matrix[i]))
        for j in range(len(matrix)):
            pointer=0
            for k in range(N):
                pointer +=matrix[k][j]
            matrix[N].append(pointer)
        for i in range(N+1):
            for j in range(N+1):
                print(str(matrix[i][j]).rjust(5), end="")
            print()
    except:break

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

Python: #!/usr/bin/python
# -*- coding: utf-8 -*-

def main():
    while(True):
        # 入力
        N = int(raw_input())
        if N == 0:
            break

        inp = []
        for x in range(N):
            input_list = map(int, raw_input().split())
            inp.append(input_list)

        printAns(inp)

def printAns(num_list):
    add_list = []
    for x in num_list:
        x.append(sum(x))
        add_list.append(x)

    total_list = [sum(x) for x in zip(*add_list)]
    add_list.append(total_list)
    print5digit(add_list)

def print5digit(num_list):
    for x in num_list:
        for y in x:
            print("{0:4d}".format(y)),
        else:
            print

if __name__=='__main__':
    main()

Python: while True:
  l = []
  n = int(input())
  if n == 0:
    break
  lx = [0 for i in range(n+1)]
  for j in range(n):
    l1 = [int(i) for i in input().split()]
    c = 0
    for i in l1:
      c += i
    l1.append(c)
    for i in range(n+1):
      lx[i] += l1[i]
    l.append(l1)
  l.append(lx)
  for s in l:
    a = ''
    for t in s:
      a += ' ' * (5-len(str(t)))
      a += str(t)
    print(a)