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
}

C++: #include<iostream>
using namespace std;

int main(){
	int n;
	while(1){
		cin>>n;
		if(n==0)break;
		int dt[n+1][n+1];
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cin>>dt[i][j];
			}
		}
		for(int x=0;x<n+1;x++){
			dt[x][n]=0;
		}
		for(int y=0;y<n;y++){
			dt[n][y]=0;
		}
		for(int k=0;k<n;k++){
			for(int l=0;l<n;l++){
				dt[k][n]+=dt[k][l];
			}
		}
		for(int x=0;x<=n;x++){
			for(int y=0;y<n;y++){
				dt[n][x]+=dt[y][x];
			}
		}
		for(int a=0;a<=n;a++){
			for(int b=0;b<=n;b++){
				if(dt[a][b]/10==0)cout<<"    "<<dt[a][b];
				else if(dt[a][b]/100==0)cout<<"   "<<dt[a][b];
				else if(dt[a][b]/1000==0)cout<<"  "<<dt[a][b];
			}
			cout<<endl;
		}
	}
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
    int N;
    while(cin>>N,N){
        int num[150][150]={{0}};
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                cin>>num[i][j];
                num[i][N]+=num[i][j];
                num[N][j]+=num[i][j];
                num[N][N]+=num[i][j];
            }
        }
        for(int i=0;i<=N;i++){
            cout<<num[i][0];
            for(int j=1;j<=N;j++){
                cout<<" "<<num[i][j];
            }
            cout<<endl;
        }
    }
    return 0;
}

C++: #include<iostream>
using namespace std ;

int main(){
	
	int n ;
	while( cin >> n , n ){
		
		int m[11][11] = {} ;
		
		for( int i=0; i<n ; i++ ){
			for( int j=0 ; j<n ; j++ ){
				cin >> m[i][j] ;
				m[i][n] += m[i][j] ;
			}
		}
		
		for( int j=0 ; j<=n ; j++ ){
			for( int i=0 ; i<n ; i++ ){
				m[n][j] += m[i][j] ;
			}
		}
		
		for( int i=0 ; i<=n ; i++ ){
			for( int j=0 ; j<=n ; j++ ){
				if( m[i][j] < 10 ) cout << ' ' ;
				if( m[i][j] < 100 ) cout << ' ' ;
				if( m[i][j] < 1000 ) cout << ' ' ;
				cout << ' ' << m[i][j] ;
			}
			cout << endl ;
		}
	}
	
return 0 ;
}

C++: #include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int n;

	while (cin >> n, n)
	{
		int table[11][11] = {};

		for (int y = 0; y < n; y++)
		{
			for (int x = 0; x < n; x++)
			{
				cin >> table[x][y];
			}
		}

		for (int y = 0; y < n; y++)
		{
			for (int x = 0; x < n; x++)
			{
				table[n][y] += table[x][y];
			}
		}
		for (int x = 0; x < n; x++)
		{
			for (int y = 0; y < n; y++)
			{
				table[x][n] += table[x][y];
			}
		}
		for (int x = 0; x < n; x++)
		{
			table[n][n] += table[x][n];
		}

		for (int y = 0; y <= n; y++)
		{
			for (int x = 0; x <= n; x++)
			{
				cout << setw(4) << table[x][y] << ' ';
			}
			cout << endl;
		}
	}

	return 0;
}

C++: #include <iostream>
using namespace std;

int main(){
  int n;
  while(1){
    cin >> n;
    if(n==0)break;

    int x[15][15]={};
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
	cin >> x[i][j];
      }
    }
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
	x[i][n] += x[i][j];
	x[n][j] += x[i][j];
	x[n][n] += x[i][j];
      }
    }
    
    for(int i = 0; i <= n; i++){
      for(int j = 0; j <= n; j++){
	if(j < n){
	  cout.width(5);
	  cout << x[i][j] << " ";
	}
	if(j==n){
	  cout.width(5);
	  cout << x[i][j] << "\n";
	}
      }
    }
  }
  
  return 0;
}

C++: #include <vector>
#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
	int n;

	while (true)
	{
		cin >> n;

		if (n == 0) { break; }

		vector<vector<int> > R(n + 1, vector<int>(n + 1, 0));

		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				cin >> R[i][j];
			}
		}

		for (int i = 0; i <= n; i++)
		{
			int sum1 = 0;
			int sum2 = 0;

			for (int j = 0; j < n; j++)
			{
				sum1 += R[i][j];
				sum2 += R[j][i];
			}

			R[i][n] = sum1;
			R[n][i] = sum2;
		}

		for (int i = 0; i <= n; i++)
		{
			for (int j = 0; j <= n; j++)
			{
				cout << setw(5) << R[i][j];
			}

			cout << endl;
		}
	}

	return 0;
}

C++: #include <iostream>
#include <cstdio>
const int MAX_N = 10;
using namespace std;

int main(void) {
  int n;
  while (1) {
    cin >> n;
    if (n == 0) break;
    int masu[MAX_N + 1][MAX_N + 1];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> masu[i][j];
      }
    }

    for (int i = 0; i < n; i++) {
      int tmp = 0;
      for (int j = 0; j < n; j++) {
        tmp += masu[i][j];
      }
      masu[i][n] = tmp;
    }
    for (int i = 0; i < n; i++) {
      int tmp = 0;
      for (int j = 0; j < n; j++) {
        tmp += masu[j][i];
      }
      masu[n][i] = tmp;
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum += masu[n][i];
    }
    masu[n][n] = sum;

    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < n; j++) {
        printf("%5d ", masu[i][j]);
      }
      printf("%5d\n", masu[i][n]);
    }
  }

  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define rp(a) while(a--)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
int main(){
	int n;
	while(cin>>n,n){
		vector<vector<int> >in(n+1,vector<int>(n+1));
		rep(i,n)rep(j,n)cin>>in[i][j];
		rep(i,n){
			int sum=0;
			rep(j,n)sum+=in[i][j];
			in[i][n]=sum;
		}
		rep(i,n+1){
			int sum=0;
			rep(j,n)sum+=in[j][i];
			in[n][i]=sum;
		}
		rep(i,n+1){
			rep(j,n+1)printf("%5d",in[i][j]);
			cout<<endl;
		}
	}
}

C++: #include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;

int main(){
int N,num[11][11]={},x;
int a;
while(1)
{cin>>N;
if(N==0)break;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
{cin>>num[i][j];num[i][N]+=num[i][j];}
}
for(int i=0;i<N;i++)
num[N][N]+=num[i][N];
for(int i=0;i<N;i++)
	for(int j=0;j<N;j++)
num[N][i]+=num[j][i];

for(int i=0;i<=N;i++){
for(int j=0;j<=N;j++)
{
a =5-( (int)log10((double)(num[i][j]) )+ 1);
for(int k=0;k<a;k++)
{cout<<" ";}
cout<<num[i][j];}
cout<<endl;

for(int i=0;i<=N;i++){
for(int j=0;j<=N;j++){
num[i][j]=0;
}
}

}
}

return 0;
}

C++: #!/usr/bin/python
# -*- coding: utf-8 -*-

def main():
    while(True):
        # 入力
        N = int(raw_input())
        if N == 0:
            break

        inp = []
        for x in range(N):
            input_list = map(int, raw_input().split())
            inp.append(input_list)

        printAns(inp)

def printAns(num_list):
    add_list = []
    for x in num_list:
        x.append(sum(x))
        add_list.append(x)

    total_list = [sum(x) for x in zip(*add_list)]
    add_list.append(total_list)
    print5digit(add_list)

def print5digit(num_list):
    for x in num_list:
        print "".join(map(lambda x_: str(x_).rjust(5), x))

if __name__=='__main__':
    main()