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: import functools

n = input()
while(n):
    list = []
    bottom_record = [0]*(n+1)

    for i in range(0, n):
        l = map(int, raw_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 = input()

Python: while True:
    n = int(input())

    if n == 0:
        break

    matrix = [[] for _ in range(n)]
    col_sum = [0 for _ in range(n + 1)]
    for i in range(n):
        matrix[i] = list(map(int, input().split()))
        matrix[i].append(sum(matrix[i]))
        for j in range(n + 1):
            col_sum[j] += matrix[i][j]

    matrix.append(col_sum)

    for i in range(n + 1):
        for j in range(n + 1):
            print('%5d' % matrix[i][j], end='')
        print()

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

Python: 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: #!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def main(readline=stdin.readline):
    fmt = '{:5d}'.format

    while 1:
        n = int(readline())
        if not n:
            exit()

        total = [0] * (n+1)

        for _ in range(n):
            s = 0
            for i, val in enumerate(int(d) for d in readline().split()):
                total[i] += val
                s += val
                print(fmt(val), end='')
            total[-1] += s
            print(fmt(s))

        print(''.join(fmt(i) for i in total))


if __name__ == '__main__':
    main()

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('{:5}'.format(j),end='')
        print('')
    for i in range(len(data)+1):
        print('{:5}'.format(sum(x[i] for x in data)),end='')
    print('')

Python: 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(' '.join(map(str, row)), sum(row))
        for (i, e) in enumerate(row):
            last[i] += e

    print(' '.join(map(str, last)), sum(last))

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)

    for array in table:
        print("".join("{:>5}".format(item) for item in array))

Python: while True:
  n = int(input())
  if not n:
    break

  mat = []
  for _ in range(n):
    lst = list(map(int, input().split()))
    lst.append(sum(lst))
    mat.append(lst)
  
  sum_lst = []
  for i in range(len(mat[0])):
    s = 0
    for j in range(n):
      s += mat[j][i]
    sum_lst.append(s)
  mat.append(sum_lst)

  for line in mat:
    put = ""
    for i in line:
      put += str(i).rjust(5)
    print(put)

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]))[-5:],
                ans[j] += num[i][j]
            print ("     " + str(sum(num[i])))[-5:]
            ans[j+1] += sum(num[i])

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

Python: # -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0102&lang=jp
"""
import sys


def solve(data):
    for d in data:
        d.append(sum(d))

    v_sum = [0] * len(data[0])
    for d in data:
        for x, n in enumerate(d):
            v_sum[x] += n
    data.append(v_sum)
    return data


def main(args):
    # data = []
    # data.append([52, 96, 15, 20])
    # data.append([86, 22, 35, 45])
    # data.append([45, 78, 54, 36])
    # data.append([16, 86, 74, 55])
    while True:
        num = int(input())
        if num == 0:
            break
        data = []
        for _ in range(num):
            data.append([int(x) for x in input().split(' ')])
        result = solve(data)
        for row in result:
            for n in row:
                print('{:5d}'.format(n), end='')
            print('')


if __name__ == '__main__':
    main(sys.argv[1:])

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]
	for i in range(n+1):
		print(" ".join(map(str,t[i])))

Python: while True:
    n = int(input())
    if n == 0:
        break
    rows = [list(map(int, input().split())) for _ in range(n)]
    for row in rows:
        print("".join(map("{0:>5}".format, row)), end="")
        print("{:>5}".format(sum(row)))
    columns = list(zip(*rows))
    print("".join(map("{0:>5}".format, list(sum(column) for column in columns))), end="")
    print("{0:>5}".format(sum(sum(row) for row in rows)))