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

int main(){
	int n;
	int i , j;
	while(cin >> n, n!=0) {
	int arr[10][10]={0,0};
	int ansW[11]={0};
	int ansH[11]={0};
		for(i=0 ; i<n ; i++){
			for(j=0 ; j <n ; j++){
				cin >>arr[i][j];
			}
		}
		for(i=0 ; i<n ; i++){
			for(j=0 ; j<n ; j++){
				ansH[i]+=arr[i][j];
			}
		}
		for(i=0 ; i<n ; i++){
			for(j=0 ; j<n ; j++){
				ansW[i]+=arr[j][i];
			}
		}
		for(i=0 ; i<n ; i++){
			for(j=0 ; j<n ; j++){
				cout.fill(' ');
				cout << setw(5) <<right <<arr[i][j];
			}
			cout << setw(5) <<right <<ansH[i] << endl;
		}
		for(i=0; i<n ; i++){
				ansW[n]+=ansW[i];
		}
		for(i=0 ; i<n+1 ; i++){
			cout << setw(5) <<right << ansW[i];
		}
		cout << endl;
	}
	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 <iostream>
#include <cstdio>

using namespace std;

int table[11][11];
int main() {
  int N;
  while (cin >> N) {
    if (N == 0) break;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        cin >> table[i][j];
      }
    }
    int total = 0;
    for (int i = 0; i < N; i++) {
      int sum = 0;
      for (int j = 0; j < N; j++) {
        sum += table[i][j];
      }
      table[i][N] = sum;
      total += sum;
    }
    for (int i = 0; i < N; i++) {
      int sum = 0;
      for (int j = 0; j < N; j++) {
        sum += table[j][i];
      }
      table[N][i] = sum;
    }
    table[N][N] = total;
    for (int i = 0; i < N+1; i++) {
      for (int j = 0; j < N+1; j++) {
        printf("%5d", table[i][j]);
      }
      printf("\n");
    }
  }

  return 0;
}

C++: 
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <utility>

#define ll (long lon
#define REP(i,n) for(int i = 0;i<(int)n;i++)
#define FOR(i,a,b) for(int i = (a);i < (b);i++)
#define RFOR(i,a,b) for(int i = (a);i>(b);i--)

using namespace std;

int main (void){
    
    int number;
    while(cin >> number,number)
    {
        int math[11][11] ={0};
        REP(i,number)
        {
            REP(j,number)
            cin >> math[i][j];
        }
        REP(i,number)
            REP(j,number)
        math[i][number] += math[i][j];
        REP(i,number)
            REP(j,number)
        math[number][i] += math[j][i];
        REP(i,number)
        REP(j,number)
        math[number][number]+= math[i][j];
        REP(j, number+1)
        REP(i,number+1){
            cout << math[j][i];
            if(i == number || (j == number && i == number))
                cout << endl;
            else
                cout << " ";
            
        }
    }
    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%c",a[i][j],j==n?10:32);
        }
    }
}

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

using namespace std;

int main()
{
	int n;
	int i,j;
	for(;;)
	{
		cin >> n ;
		if(n==0)
			break;
		int table[12][12]={0};
		
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
			{
				cin >> table[i][j] ;
			}
		}
		
		for(i=0;i<=n;i++)
		{
			for(j=0;j<=n;j++)
			{
				if(j!=n)
				{
					printf("%3d ",table[i][j]);
					table[i][n]+=table[i][j];
					table[n][j]+=table[i][j];
				}
				else
					printf("%3d",table[i][j]);
			}
			cout << endl;
		}
	}
}

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

using namespace std;

//define
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define dump(x)  cerr << #x << " = " << (x) << endl;

#define INF (INT_MAX/3)
#define PI (2*acos(0.0))
#define EPS (1e-8)

typedef long long ll;
typedef unsigned long long ull;

int main(){
  ios_base::sync_with_stdio(0);
  int n;
  while(cin >> n, n){
    vector< vector<int> > mt;
    for(int i = 0; i < n; i++){
      vector<int> c;
      int sum = 0;
      for(int j = 0; j < n; j++){
        int a;  cin >> a;
        c.push_back(a);
        sum += a;
      }
      c.push_back(sum);
      mt.push_back(c);
    }
    vector<int> lc;
    for(int i = 0; i < n + 1; i++){
      int sum = 0;
      for(int j = 0; j < n; j++) sum += mt[j][i];
      lc.push_back(sum);
    }
    mt.push_back(lc);
    for(int i = 0; i < n + 1; i++){
      for(int j = 0; j < n + 1; j++){
        printf("%5d", mt[i][j]);
      }
      cout << endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<iomanip>
using namespace std;
int main(void)
{
    int n,A=0,ober[10][10],side[10],lon=0;
    while(1){
        for(int i=0;i<12;i++)side[i]=0;
        cin>>n;
        if(n==0)break;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cin>>ober[i][j];
                side[j]+=ober[i][j];
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cout<<setw(5)<<ober[i][j];
                A+=ober[i][j];
            }
            cout<<setw(5)<<A<<"\n";
            lon+=A;
            A=0;
        }
        for(int i=0;i<n;i++)cout<<setw(5)<<side[i];
        cout<<setw(5)<<lon<<"\n";
        lon=0;
    }
}

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

int n;
int field[1000][1000];
int total;

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0) return 0;

    total = 0;
    for (int i=0; i<n; i++) {
      for (int j=0; j<n; j++) {
        scanf("%d", &field[i][j]);
        total += field[i][j];
      }
    }

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

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

    for (int i=0; i<n+1; i++) {
      printf("%4d", field[i][0]);
      for (int j=1; j<n+1; j++) {
        printf(" %4d", field[i][j]);
      }
      puts("");
    }
  }
}