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>

using namespace std;

int main() {
    for (;;) {
    int n;
    cin >> n;
    if (n == 0) return 0;

    int matrix[n+1][n+1];

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

    for (int i = 0; i < n+1; i++) {
        matrix[n][i] = 0;
        matrix[i][n] = 0;
    }
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            matrix[i][n] += matrix[i][j];
        }
    }

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

    matrix[n][n] = 0;
    for (int i = 0; i < n; i++) {
        matrix[n][n] += matrix[n][i];
    }
    
    for (int i = 0; i < n+1; i++) {
        for (int j = 0; j < n+1; j++) {
            cout.width(3);
            if (j == n) 
                cout << matrix[i][j];
            else 
                cout << matrix[i][j] << ' ';
        }
        cout << endl;
    }
    }

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define all(vec) vec.begin(),vec.end()
typedef long long int ll;
typedef pair<ll,ll> P;
const ll MOD=10000;
const ll INF=1000000010;
const ll LINF=1000000000000000010;
const int MAX=100001;
int dx[8]={0,1,0,-1,1,-1,1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
ll keta(ll n){
    int d=0;
    while(n>0){
        n/=10;
        d++;
    }
    return d;
}
int main(){
    int n;
    while(cin>>n,n){
        int a[12][12]={};
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cin>>a[i][j];
                a[i][n]+=a[i][j];
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                a[n][i]+=a[j][i];
            }
        }
        for(int i=0;i<n;i++){
            a[n][n]+=a[n][i];
        }
        for(int i=0;i<=n;i++){
            for(int j=0;j<=n;j++){
                for(int k=keta(a[i][j]);k<5;k++){
                    cout<<" ";
                }
                cout<<a[i][j];
            }
            cout<<endl;
        }
    }
	return 0;
}

C++: #include<iostream>
using namespace std;
int main() {
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		int data[150][150] = { 0 };

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

		}
		for (int i = 0;i < n+1;i++) {
			for (int j = 0;j < n + 1;j++) {
				if (data[i][j] < 10) {
					cout << "    " << data[i][j];
				}
				else if (data[i][j] < 100) {
					cout << "   " << data[i][j];
				}
				else if (data[i][j] < 1000) {
					cout << "  " << data[i][j];
				}
			}
			cout << endl;
		}
	}
	return 0;

}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;







int main() {
	int n;
	while (cin >> n&&n > 0) {
		vector<vi> v(n + 1, vi(n + 1, 0));
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> v[i][j];
				v[i][n] += v[i][j];
				v[n][j] += v[i][j];
			}
			v[n][n] += v[i][n];
		}
		for (int i = 0; i <= n; i++) {
			for (int j = 0; j <= n; j++) {
				printf("%5d", v[i][j]);
			}
			cout << endl;
		}
	}
}

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

int main(){

  int n;
 
  while(1){
    scanf("%d",&n);
    if(n==0) break;
    
    int tbl[11][11]={{0}};
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	scanf("%d",&tbl[i][j]);
	tbl[i][n] += tbl[i][j];
	tbl[n][j] += tbl[i][j];
	tbl[n][n] += tbl[i][j];
      }
    }
    
    for(int i=0;i<=n;i++){
      for(int j=0;j<=n;j++){
	printf("%5d",tbl[i][j]);
      }
      printf("\n");
    }
  }

  return 0;

}

C++: #include <iostream>
using namespace std;
int main(){
        int N;
        while(true){
        cin >> N;
        if(N==0)
                break;
        int table[100][100];
        for(int i=0 ; i<N ; i++ ){
                int sum=0;
                for(int j=0 ; j<N ; j++ ){
                        cin >> table[i][j];
                        sum+=table[i][j];
                }
                table[i][N]=sum;
        }
        for(int i=0 ; i<=N ; i++ ){
                table[N][i]=0;
                for(int j=0 ; j<N ; j++ ){
                        table[N][i]+=table[j][i];
                }
        }
        for(int i=0 ; i<=N ; i++ ){
                for(int j=0 ; j<=N ; j++ ){
                        cout << table[i][j];
                        if(j!=N)
                                cout<<" ";
                }
                cout << endl;
        }
        }
        return 0;
}

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

using namespace std;

void solve()
{
	int N;
	while(cin >> N, N)
	{
		vector< vector<int> > Vec(N + 1, vector<int>(N + 1));
		for(int i = 0; i < N; ++i)
		{
			for(int j = 0; j < N; ++j)
			{
				cin >> Vec[i][j];
			}
		}
		for(int i = 0; i < N; ++i)
		{
			for(int j = 0; j < N; ++j)
			{
				Vec[i][N] += Vec[i][j];
				Vec[N][i] += Vec[j][i];
			}
		}
		for(int i = 0; i < N; ++i)
		{
			Vec[N][N] += Vec[i][N];
		}
		for(int i = 0; i < N + 1; ++i)
		{
			for(int j = 0; j < N; ++j)
			{
				printf("%5d ", Vec[i][j]);
			}
			printf("%5d\n", Vec[i][N]);
		}
	}
}

int main()
{
	solve();
	return(0);
}

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

int main(){
    int n = 0;
    while (1){
	cin >> n;
	if (!n) break;
	vector<vector<int> > matrix(n+1, vector<int>(n+1));
	int input;
	for (int a = 0; a < n; a++){
	    matrix[a][n] = 0;
	    for (int b = 0; b < n; b++){
		cin >> matrix[a][b];
		matrix[a][n] += matrix[a][b];
	    }
	}
	for (int a = 0; a < n; a++){
	    for (int b = 0; b < n; b++){
		matrix[n][b] += matrix[a][b];
	    }
	}
	for (int b = 0; b < n; b++)
	    matrix[n][n] += matrix[n][b];

	for (int a = 0; a < n+1; a++){
	    for (int b = 0; b < n+1; b++)
		cout << setw(5) << matrix[a][b] << " ";
	    cout << '\n';
	}
    }
}