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(True):
    n = int(input())
    if n == 0:
         break
    arr = []
    for i in range(n):
        arr.append(list(map(int, input().split())))
    arr = [ l+[sum(l)] for l in arr]
    brr = [0]*(n+1)
    for i in range(n):
        for j in range(n+1):
            brr[j] += arr[i][j]
    crr = arr+[brr]
    for c in crr:
        for d in c:
            print("%5d"%d,end="")
        print()

Python: while True:
    n = int(input())
    if n == 0:
        break
    total = [0] * (n + 1)
    for i in range(n):
        a = [int(i) for i in input().split()]
        a.append(sum(a))
        print(" ".join(map(str, a)))
        for j in range(n + 1):
            total[j] += a[j]
    print(" ".join(map(str, total)))

Python: import sys
#from me.io import dup_file_stdin

def print2d(m):
    [print(" ".join(map(str,r))) for r in m]

#@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: while 1:
    n=int(raw_input())
    if n==0:break
    a=[map(int,raw_input().split()) for _ in xrange(n)]
    ans=[[0]*(n+1) for _ in xrange(n+1)]
    for i in xrange(n+1):
        for j in xrange(n+1):
            if i==n :
                if j==n:
                    ans[n][n]+=sum(ans[n])
                else:
                    for k in xrange(n):
                        ans[n][j]+=a[k][j]
            else:
                if j==n:
                    for k in xrange(n):
                        ans[i][n]+=a[i][k]
                else:
                    ans[i][j]=a[i][j]
    for i in xrange(n+1):
        print(' '.join(map(str,ans[i])))

Python: import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop
import copy
from test.support import _MemoryWatchdog

BIG_NUM = 2000000000
HUGE_NUM = 99999999999999999
MOD = 1000000007
EPS = 0.000000001
sys.setrecursionlimit(100000)


SIZE = 51
table = [[None]*SIZE for _ in range(SIZE)]

while True:
    N = int(input())
    if N == 0:
        break

    for row in range(N+1):
        for col in range(N+1):
            table[row][col] = 0

    sum = 0

    for row in range(N):
        input_array = list(map(int,input().split()))
        for col in range(N):
            table[row][col] += input_array[col]
            table[N][col] += input_array[col]
            table[row][N] += input_array[col]
            sum += input_array[col]

    table[N][N] = sum

    for row in range(N+1):
        for col in range(N+1):
            print("%5d"%(table[row][col]),end="")
        print()

Python: # -*- coding: utf-8 -*-

import sys
import os

for s in sys.stdin:
    n = int(s)
    if n == 0:
        break

    M = []
    for i in range(n):
        lst = list(map(int, input().split()))
        lst.append(sum(lst))
        M.append(lst)

    last_lst = []
    # vertical sum
    for i in range(n+1):
        sumv = 0
        for row in M:
            sumv += row[i]
        last_lst.append(sumv)
    M.append(last_lst)

    # answer
    for row in M:
        for val in row:
            print(str(val).rjust(5), end='')
        print()

Python: while 1:
    n = int(input())
    if n == 0:
        break
    L = []
    for i in range(n):
        L += [list(map(int,input().split()))+[0]]
    L += [[0]*(n+1)]
    for r in range(n):
        for c in range(n):
            L[r][n] += L[r][c]
            L[n][c] += L[r][c]
            L[n][n] += L[r][c]
    for f in range(n+1):
        for h in range(n+1):
            L[f][h] = str(L[f][h]).rjust(5)
        print (''.join(L[f]))

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

n=int(input())
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 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: #!/usr/bin/env python3

import sys

input_lines = [list(map(int, i.split(' '))) for i in sys.stdin.readlines()]

#print(input_lines)
cnt = 0
lastline = []
output = []
for i in input_lines:
	if len(i) == 1:
		cnt = i[0]
		lastline = [0 for i in range(cnt+1)]

	else:
		i.append(sum(i))
		for k in range(len(i)):
			lastline[k] += i[k]
		cnt -= 1
		output.append(i)

	if cnt == 0:
		output.append(lastline)
		
s = ""
for i in output[:-1]:
	for j in i:
		s += "{:>5}".format(j)
	s+='\n'

print(s[:-1])

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('{0:5d}'.format, l))) 
    print(''.join(map('{0:5d}'.format, bottom_record)))
    n = int(input())

Python: while 1:
    n = int(input())
    if n == 0:
        break

    mat = [[0 for i in range(n)] for j in range(n)]
    for i in range(n):
        mat[i] = list(map(int, input().split()))

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

    col = [0] * n
    for i in range(n):
        tmp = 0
        for j in range(n):
            tmp += mat[j][i]
        col[i] = tmp

    ans = sum(col)
    col.append(ans)

    for i in range(n):
        print(''.join(str(ans).rjust(5) for ans in mat[i]))

    print(''.join(str(ans).rjust(5) for ans in col))