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 _ 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: 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: while True:
    n = int(input())
    if n == 0:
        break

    a = []
    for i in range(n):
        a.append([int(num) for num in input().split()])

    for i in range(n):
        sum = 0
        for j in range(n):
            sum += a[i][j]
        a[i].append(sum)


    a.append([])
    for i in range(n + 1):
        sum = 0
        for j in range(n):
            sum += a[j][i]
        a[n].append(sum)

    for i in range(n + 1):
        for j in range(n + 1):
            print('{0:>5}'.format(a[i][j]), end="")
        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), end='')

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: while True:
    rows = []
    total = {}
    n = int(input())
    if (n == 0):
        break

    for r in range(n):
        datas = map(int, input().split())
        ds = list(datas)
        rows.append(ds)
            
    for r in range(n):
        s = 0
        for c in range(n):
            s += rows[r][c]
            if (c not in total):
                total[c] = rows[r][c]
            else:
                total[c] += rows[r][c]

        rows[r].append(s)
        if (n not in total):
            total[n] = s
        else:
            total[n] += s

    for r in range(n):
        for c in range(len(rows[r])):
            print("{0:>5}".format(rows[r][c]), end="")
        print()

    for t in range(len(total)):
        print("{0:>5}".format(total[t]), end="")
    print()

Python: import numpy

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)
			else:
				print('{:>5}'.format(data[i][j]))

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("{:<10}".format("%s")%((" ").join(list(map(str,list_row)))))
        print("{:<10}".format("%s") % ((" ").join(list(map(str, sum_col)))))
    else:
        break

Python: def rowSum(matrix):
  n=len(matrix)
  sumcolumn=[0]*n
  for i in range(0,n):
    sumcolumn[i] = sum(matrix[i])
  return sumcolumn

def colSum(matrix):
  n=len(matrix)
  sumrow=[0]*n
  for i in range(0,n):
    for j in range(0,n):
      sumrow[i]+=matrix[j][i]
  return sumrow

while True:
  n=int(input())
  if n==0:  break
  matrix=[None]*n
  for i in range(0,n):
    matrix[i] = list(map(int,input().split()))

  c=rowSum(matrix)
  r=colSum(matrix)

  matrix.append(r)

  for i in range(0,n):
    matrix[i].append(c[i])

  matrix[n].append(sum(matrix[n]))

  for row in matrix:
    line=""
    for element in row:
      line += '{:>5}'.format(str(element))
    line.rstrip()
    print(line)

Python: 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:
            print("{:5d} ".format(r), end="")
        print("{:5d}".format(sum(row)))
    columns = list(zip(*rows))
    for column in columns:
        print("{:5d} ".format(sum(column)), end="")
    print("{:5d}".format(sum(sum(row) for row in rows)))

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)

    output = []
    for array in table:
        content = ["{:>5}".format(str(item)) for item in array]
        output.append(str(content))

    print("\n".join(output))

Python: def output_res(row):
    print(''.join(map(lambda num: str(num).rjust(5), row)), str(sum(row)).rjust(5), sep='')


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

        bottom_total = [0 for _ in range(n)]
        for _ in range(n):
            each_line = list(map(int, input().split(' ')))
            output_res(each_line)
            bottom_total = [x + y for x, y in zip(bottom_total, each_line)]

        output_res(bottom_total)


if __name__ == '__main__':
    main()

Python: # AOJ 0102: Matrix-like Computation
# Python3 2018.6.17 bal4u

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