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

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); i++ )
#define UREP( i, m, n ) for ( unsigned int i = (unsigned int)( m ); i < (unsigned int)( n ); i++ )

#define ITER( c ) __typeof( (c).begin() )
#define IREP( c, it ) for ( ITER(c) it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()
#define RALL( c ) (c).rbegin(), (c).rend()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl


int main()
{
	while ( true )
	{
		int n;
		cin >> n;

		if ( n == 0 )
		{
			break;
		}

		vector< vector<int> > table;

		REP( i, 0, n )
		{
			table.PB( vector<int>() );
			int sum = 0;

			REP( j, 0, n )
			{
				int in;
				cin >> in;
				table[i].PB( in );
				sum += in;
			}

			table[i].PB( sum );
		}

		table.PB( vector<int>() );

		REP( i, 0, n + 1 )
		{
			int sum = 0;

			REP( j, 0, n )
			{
				sum += table[j][i];
			}

			table[n].PB( sum );
		}

		REP( i, 0, table.size() )
		{
			REP( j, 0, table[i].size() )
			{
				printf("%5d%c", table[i][j], ( j + 1 == table[i].size() ? '\n' : ' ' ) );
			}
		}
	}

	cout << endl;

	return 0;
}

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

using namespace std;

int matrix[11][11];

int main()
{
	int n,sum;
	
	for(;;)
	{
		cin>>n;
		if(n==0)break;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				cin>>matrix[i][j];
			}
		}

		for(int i=0;i<n;i++)
		{
			sum=0;
			for(int j=0;j<n;j++)
			{
				sum+=matrix[i][j];
			}
			matrix[i][n]=sum;
		}
		for(int j=0;j<n;j++)
		{
			sum=0;
			for(int i=0;i<n;i++)
			{
				sum+=matrix[i][j];
			}
			matrix[n][j]=sum;
		}
		sum=0;
		for(int i=0;i<n;i++)
		{
			sum+=matrix[i][n];
		}
		matrix[n][n]=sum;
		
		for(int i=0;i<=n;i++)
		{
			for(int j=0;j<=n;j++)
			{
				if(j>0)cout<<' ';
				cout<<setfill(' ')<<setw(5)<<right<<matrix[i][j];
			}
			cout<<endl;
		}
	}
}

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

int main(){
	double num[12][12]={};
	int N,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++)
{
if(num[i][j]==0)a==4;
else if(num[i][j]>0)a =5-( (int)log10((double)(num[i][j]) )+ 1);
else if(num[i][j]<0)a =4-( (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++: #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;
}