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>
#include <cstdio>
#include <cstring>

using namespace std;

int main(int argc, char *argv[]) {
  int n;
  int data[11][11];
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    memset(data, 0, sizeof(int) * 11 * 11);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        cin >> data[i][j];

    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        data[n][i] += data[j][i];
    for (int i = 0; i < n+1; i++)
      for (int j = 0; j < n; j++)
        data[i][n] += data[i][j];

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

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;
}