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

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

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

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

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

    for i in range(len(arr[i])-1):
        for j in range(len(arr[i])):
            print('{0:5d}'.format(arr[i][j]),end = '')
        print('')
    for i in range(len(last_line)):
        print('{0:5d}'.format(last_line[i]),end = '')
    print('')