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 1:
 n=int(input())
 if n==0:break;
 a=[]
 for i in range(n):
  a[i]=[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:
 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: 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(str(j)+' ',end='')
        print('')
    for i in range(len(data)+1):
        print(str(sum(x[i] for x in data))+' ',end='')
    print('')

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

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

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: from sys import stdin

if __name__ == '__main__':
    for n in stdin:
        if int(n) == 0:
            break

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

Python: import functools

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

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

Python: while True:
    n = int(input())
    if n != 0:
        sum_col = [0 for i in range(n+1)]
        for i in range(n):
            list_row = list(map(int,input().split(" ")))
            sum_row = sum(list_row)
            list_row.append(sum_row)
            for j in range(len(list_row)):
                sum_col[j] += list_row[j]


            print((" ").join(list(map(str,list_row))))
        print((" ").join(list(map(str,sum_col))))
    else:
        break

Python: n=int(input())
def sum1(x,y):
    num=0
    for i in range(n):
        num+=x[i][y]
    return int(num)
            
Matrix=[]
for i in range(n+1):
    Matrix.append(i)
    if i==n:
        Matrix[i]=[]
        for j in range(5):
            Matrix[i].append(sum1(Matrix,j))
    else:
        Matrix[i]=[int(k) for k in input().split()]
        Matrix[i].append(sum(Matrix[i]))

for i in range(n+1):
    for j in range(5):
        print(Matrix[i][j],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])]
        print(*a[i])
    a=list(zip(*a[::-1]))
    for i in range(n):print(sum(a[i]),end=' ')
    print(sum(a[n]))

Python: import sys
f=lambda v,b: v.append(sum(b))
while True:
    c=input()
    if c==0: break
    x=range(c);z=range(c+1);q={}
    for i in x:
        q[i]=map(int,raw_input().split());t=q.values()[i];f(t,t)
    q[i+1]=[]
    for g in x:
        w=[]
        for j in x:
            w.append(q.values()[j][g]) 
        f(q[i+1],w)
    t=q[i+1];f(t,t)
    for i in q.values():
        for j in z: sys.stdout.write("%5d"%i[j])
        print

Python: def print_row(iterable):
    print(''.join(map(lambda x: str(x).rjust(5), iterable)),
          str(sum(iterable)).rjust(5), sep='')


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_row(row)
        for (i, e) in enumerate(row):
            last[i] += e

    print_row(last)

Python: import sys
def main():
    for line in sys.stdin:
        n = int(line)
        if n != 0:
            a = []
            for _ in range(n):
                a.append(list(map(int, input().split())))
            a.append(list(range(n + 1)))

            for x in range(n):
                hoge = 0
                for y in range(n):
                    hoge += a[y][x]
                else:
                    a[n][x] = hoge

            for x in range(n + 1):
                hoge = 0
                for y in range(n):
                    hoge += a[x][y]
                else:
                    if x != n:
                        a[x].append(hoge)
                    else:
                        a[n][n] = hoge

            for x in range(n + 1):
                for y in range(n + 1):
                    print("{0:5d}".format(a[x][y]), end = "")
                else:
                    print()

        else:
            break


if __name__ == "__main__":
    main()

Python: # Aizu Problem 0102: Matrix-like Computation
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


while True:
    N = int(input())
    if N == 0:
        break
    M = [[int(_) for _ in input().split()] for __ in range(N)]
    for i in range(N):
        M[i].append(sum(M[i]))
    last = [sum([M[row][col] for row in range(N)]) for col in range(N + 1)]
    M.append(last)
    fmt = "%5d" * (N + 1)
    for row in M:
        print(fmt % tuple(row))

Python: def print_list_inline(tr_list):
    inline = map(lambda string:"{:>5}".format(string),tr_list)
    inline = list(inline)
    print(("").join(inline))


while True:
    n = int(input())
    if n != 0:
        sum_col = [0 for i in range(n+1)]
        for i in range(n):
            list_row = list(map(int,input().split(" ")))
            sum_row = sum(list_row)
            list_row.append(sum_row)
            for j in range(len(list_row)):
                sum_col[j] += list_row[j]
            print_list_inline(list_row)
        print_list_inline(sum_col)
    else:
        break