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

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) < i + 1:
                    bottom_total.append(int(v))
                else:
                    bottom_total[i] += 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(bottom_total)).rjust(5))

Python: while(1):
	box = []
	num = int(input())
	if num == 0: break
	[box.append(list(map(int,input().split()))) for i in range(num)]
	[box[i].append(sum(box[i])) for i in range(num)]
	box_replace = list(map(list, zip(*box)))
	temp = [[]]
	[temp[0].append(sum(box_replace[i])) for i in range(num+1)]
	box += temp
	box = [list(map(str, box[i])) for i in range(num+1)]
	box = ([[ box[i][j].rjust(5," ") for j in range(num+1)] for i in range(num+1)] )
	
	[print("".join(box[i])) for i in range(num+1)]

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

        for i in range(n):
            num = 0
            m = map(int, input().split())
            for j in m:
                print("{:5}".format(j), end='')
                num += j
            print("{:5}".format(num))

if __name__ == '__main__':
    main()

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

    for i in range(0, n):
        l = list(map(int, input().split()))
        l.append(sum(l))
        bottom_record = [x + y for(x,y) in zip(bottom_record, l)]
        print(''.join(map('{0:5d}'.format, l))) 
    print(''.join(map('{0:5d}'.format, bottom_record)))
    n = int(input())