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

Python: while True:
    data=[]
    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:
  a=[list(map(int,input().split()))for _ in[0]*n]
  for r in a:
   for s in r:print(f"{s:5}",end='')
   print(f"{sum(r):5}")
  for c in zip(*a):print(f"{sum(c):5}",end='')
  print(f"{sum([sum(r)for r in a]):5}")

Python: while True:
  n = int(input())
  if n==0:
    break
  A=[]
  for a in range(n):
    x =list(map(int, input().split()))
    x.append(sum(x))
    A.append(x)
  Y=[]
  for j in range(n+1):
    y=0
    for i in range(n):
      y+=A[i][j]
    Y.append(y)

  A.append(Y)
  
  for i in range(n+1):
    for j in range(n+1):
      a=A[i][j]
      a=str(a)
      a=a.rjust(5)
      print(a,end="")
    print()

Python: while 1:
 n=int(input())
 if n==0:break;
 a=[list(map(int,input().split()))for _ in[0]*n]
 for r in a:
  for s in r:print(f"{s:5}",end='')
  print(f"{sum(r):5}")
 for c in zip(*a):print(f"{sum(c):5}",end='')
 print(f"{sum([sum(r)for r in a]):5}")

Python: while True:
    data=[]
    n=int(input())
    if n==0:break
    for i in range(n):
        a=list(map(int,input().split(' ')))
        a.append(sum(a))
        data.append(a)
    a=[]
    for i in range(n+1):
        a.append(sum(x[i] for x in data))
    data.append(a)
    for i in range(n+1):
        for j in range(n+1):
            print('{:5}'.format(data[i][j]),end='')
        print('')

Python: import sys
#from me.io import dup_file_stdin

def print2d(m):
    for r in m:
        print("".join(["{:5d}".format(x) for x in r]))

#@dup_file_stdin
def solve():
    while True:
        n = int(sys.stdin.readline())
        if n == 0 :
            return
        m = []
        for i in range(n):
            m.append([int(x) for x in sys.stdin.readline().split(' ')])
        for r in range(n):
            m[r].append(sum(m[r]))
        lastRow = []
        for c in range(n+1):
            s = 0
            for r in range(n):
                s += m[r][c]
            lastRow.append(s)
        m.append(lastRow)
        print2d(m)
solve()

Python: if __name__ == '__main__':

	while True:

		n = int(input())
		
		if n == 0:
			break

		A = [[0 for _ in range(n+1)]for _ in range(n)]
		B = [0 for _ in range(n+1)]

		for i in range(n):
			tmp = list(map(int,input().split()))
			
			for j,k in enumerate(tmp):
				A[i][j] += k
				B[j] += k

			A[i][n] = sum(A[i])
			B[n] = sum(B[:n])
		
		for m in A:
			tmp2 = ""
			for g in m:
				tmp2 += '{:>5}'.format(str(g))
			print(tmp2)
		tmp3 = ""
		for y in B:
			tmp3 += '{:>5}'.format(str(y))
		print(tmp3)

Python: while 1:
  l=[list(map(int,input().split())) for i in range(int(input()))]
  if not l:break
  le=len(l)
  for (i,j) in enumerate(l):
    l[i].append(sum(j))
  l.append([sum([l[k][i] for k in range(le)]) for i in range(le+1)])
  [print("".join([str(j).rjust(5) for j in i])) for i in l]

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("{0:>5}".format,t[i])))

Python: while 1:
 n=int(input())
 if n==0:break;
 a=[list(map(int,input().split()))for _ in[0]*n]
 for r in a:
  for s in r:print(f"{s:5}",end='')
  print(f"{sum(r):5}")
 for c in zip(*a):print(f"{sum(c):5}",end='')
 print(f"{sum([sum(r)for r in a]):5}")

Python: import sys

while True:
    n = int(raw_input())
    if n == 0: break
    
    a = []
    for i in range(n):
        a.append(map(int, raw_input().split()))
    
    for i in range(n):
        a[i].append(sum(a[i]))
    
    col_sum = []
    for col in range(len(a[0])):
        s = 0
        for row in range(len(a)):
            s += a[row][col];
        col_sum.append(s)
    a.append(col_sum)

    for row in range(len(a)):
        for col in range(len(a[0])):
            sys.stdout.write("%5d" % a[row][col])
        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 n == 0:
        break
    ans = []
    sum_c = [0 for i in range(n + 1)]
    for i in range(n):
        r = list(map(int, input().split()))
        r.append(sum(r))
        ans.append(r)
        for j in range(n + 1):
            sum_c[j] += r[j]
    ans.append(sum_c)
    for i in range(n + 1):
        [print("{:>5}".format(str(ans[i][j])),end="") for j in range(n + 1)]
        print()