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 <vector>
using namespace std;

int main(){
	int data_size;
	cin >> data_size;
	while (data_size){
		vector<vector<int>> data;
		vector<vector<int>> data_digit;
		data.resize((data_size + 1)*(data_size + 1));
		data_digit.resize((data_size + 1) * (data_size + 1));
		for (int i = 0; i < data_size; i++){
			data[i].resize(data_size + 1);
			data_digit[i].resize(data_size + 1);
			for (int j = 0; j < data_size; j++){
				cin >> data[i][j];
				int tmp = data[i][j];
				data_digit[i][j] = 0;
				while (tmp){
					tmp /= 10;
					data_digit[i][j]++;
				}
			}
		}
		data[data_size].resize(data_size + 1);
		data_digit[data_size].resize(data_size + 1);
		for (int i = 0; i < data_size; i++){
			int sum = 0;
			for (int j = 0; j < data_size; j++){
				sum += data[i][j];
			}
			data[i][data_size] = sum;
			int tmp = sum;
			data_digit[i][data_size] = 0;
			while (tmp){
				tmp /= 10;
				data_digit[i][data_size]++;
			}

		}
		for (int i = 0; i < data_size + 1; i++){
			int sum = 0;
			for (int j = 0; j < data_size; j++){
				sum += data[j][i];
			}
			data[data_size][i] = sum;
			int tmp = sum;
			data_digit[data_size][i] = 0;
			while (tmp){
				tmp /= 10;
				data_digit[data_size][i]++;
			}
		}

		for (int i = 0; i < data_size + 1; i++){
			for (int j = 0; j < data_size + 1; j++){
				for (int k = data_digit[i][j]; k < 5; k++){
					cout << " ";
				}
				cout << data[i][j];
			}
			if (i!=data_size)
			cout << "\n";
		}
		
		cin >> data_size;
		if (data_size != 0){
			cout << "\n";
		}
	}
	return 0;
}

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

int main(){
  int n,w,h,sup[10][10],wt[10],ht[10],at;
	
	while(1){
		w=0;
		h=0;
		at=0;
		
		cin >> n;
		
		if(n==0) break;
		
		for(h=0; h<n; h++){

			wt[h] = 0;

			for(w=0; w<n; w++){
				if(h==0) ht[w]=0;

				sup[h][w] = 0;

				cin >> sup[h][w];
				
				wt[h] += sup[h][w];
				
				ht[w] += sup[h][w];
			}
		}
		
		for(h=0; h<=n; h++){
			for(w=0; w<=n; w++){
				if(h<n && w<n){
					printf("%5d",sup[h][w]);
					
					at += sup[h][w];
				}
				
				else if(w==n && h<n){
					printf("%5d \n",wt[h]);
				}
				
				else if(h==n && w<n){
					printf("%5d",ht[w]);
				}
				
				else{
					printf("%5d \n",at);
				}
			}
		}
		
	}
  
  return 0;
}

C++: #include<ios>
#include<iostream>
using namespace std;
int n,a[11][11];
main()
{
    while(cin>>n,n)
    {
        for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>a[i][j];
        for(int i=0;i<n;i++)
        {
            int sum=0;
            for(int j=0;j<n;j++)sum+=a[i][j];
            a[i][n]=sum;
        }
        for(int j=0;j<=n;j++)
        {
            int sum=0;
            for(int i=0;i<n;i++)sum+=a[i][j];
            a[n][j]=sum;
        }
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)printf("%5d",a[i][j]);
            puts("");
        }
    }
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
int main(){
    while(true){
        int n;
        scanf("%d",&n);
        if(n==0) return 0;
        int a[n+1][n+1];
        fill(a[0],a[0]+(n+1)*(n+1),0);
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                scanf("%d",&a[i][j]);
                a[i][n]+=a[i][j];
                a[n][j]+=a[i][j];
                a[n][n]+=a[i][j];
                printf("%5d",a[i][j]);
            }
            printf("%5d\n",a[i][n]);
        }
        for(int i=0;i<=n;i++){
            printf("%5d",a[n][i]);
        }
        printf("\n");
    }
}

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

int main(void){
     int n;
     int ary[11][11];
     while(cin >> n,n){
	  for(int i=0;i<n;i++){
	       for(int j=0;j<n;j++){
		    ary[i][j]=0;
	       }
	  }
	  for(int i=0;i<n;i++){
	       for(int j=0;j<n;j++){
		    cin >> ary[i][j];
		    ary[i][n] += ary[i][j];
		    ary[n][j] += ary[i][j];
		    ary[n][n] += ary[i][j];
	       }
	  }
	  for(int i=0;i<n+1;i++){
	       for(int j=0;j<n+1;j++){
		    printf("%5d",ary[i][j]);
	       }
	       cout << endl;
	  }
     }
     return 0;
}

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

int main(){
  int n;
  while(cin >> n,n){
    vector< vector<int> > v(n,vector<int>(n));
    
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	cin >> v[i][j];
      }
    }
  
    vector<int> sum(n+1,0);
    for(int i=0;i<n;i++){
      int tmp = 0;
      for(int j=0;j<n;j++){
	tmp += v[i][j];
	sum[j] += v[i][j];
      }
      v[i].push_back(tmp);
      sum[n] += tmp;
    }
    v.push_back(sum);
    
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++){
	cout << setw(5) << v[i][j];
      }
      cout << endl;
    }
  }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <stack>
#include <functional>
#include <queue>
#include <cmath>
#include <set>

#define rep(i,j,k) for(int i=j;i<k;i++)
#define drep(n,m) for(int i=0;i<n;i++)for(int j=0;j<m;j++)
#define Sort(x) sort((x).begin(),(x).end())
#define fi first
#define se second
#define vi vector<int>
#define INF 1000000000
#define MOD 1000000009
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef unsigned long long ull;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};

using namespace std;

int main() {
    int n;
    while(1){
        cin>>n;
        if(n==0)return 0;
        vector<vector<int>> v(n+1,vector<int>(n+1,0));
        
        rep(i,0,n){
            int sum=0;
            rep(j,0,n){
                cin>>v[i][j];
                sum+=v[i][j];
            }
            v[i][n]=sum;
        }
        
        rep(j,0,n+1){
            int sum=0;
            rep(i,0,n){
                sum+=v[i][j];
            }
            v[n][j]=sum;
        }
        
        rep(i,0,n+1){
            rep(j,0,n+1){
                if(j==0)printf("%5d",v[i][j]);
                else printf(" %4d",v[i][j]);
            }
            cout<<endl;
        }
        
        
    }
    return 0;
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
using namespace std;

int main() {
	int n;
	while (cin >> n&&n) {
		int g[15][15];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> g[i][j];
			}
		}
		for (int i = 0; i < n; i++) {
			int sum = 0;
			for (int j = 0; j < n; j++) {
				sum += g[i][j];
			}
			g[i][n] = sum;
		}
		for (int j = 0; j < n; j++) {
			int sum = 0;
			for (int i = 0; i < n; i++) {
				sum += g[i][j];
			}
			g[n][j] = sum;
		}
		int ssum = 0;
		for (int i = 0; i < n; i++) {
			ssum += g[i][n];
		}
		g[n][n] = ssum;
		for (int i = 0; i <= n; i++) {
			for (int j = 0; j <= n; j++) {
				if (g[i][j] < 10)cout << "    " << g[i][j];
				else if (g[i][j] < 100)cout << "   " << g[i][j];
				else if (g[i][j] < 1000)cout << "  " << g[i][j];
				else if (g[i][j] < 10000)cout << " " << g[i][j];
				else cout << g[i][j];
			}
			cout << endl;
		}
	}
}