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<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,x,n) for(int i=(x);i<(n);i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define ub upper_bound
#define lb lower_bound
#define int long long
using namespace std;
const int MOD=1000000007;
const int INF=1000000009;


signed main(){
  int n;
  int M[12][12];
  while(cin>>n,n){
    rep(i,n)rep(j,n)cin>>M[i][j];
    rep(i,n){
      int tmp=0;
      rep(j,n)tmp+=M[i][j];
      M[i][n]=tmp;
    }
    rep(i,n){
      int tmp=0;
      rep(j,n)tmp+=M[j][i];
      M[n][i]=tmp;
    }
    int tmp=0;
    rep(i,n)tmp+=M[n][i];
    M[n][n]=tmp;
    rep(i,n+1){
      rep(j,n+1)cout<<setw(5)<<M[i][j];
      cout<<endl;
    }
  }
  return 0;
}

C++: #include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<cstdlib>
#include<iomanip>
#include<queue>


using namespace std;

void Solution()
{
    int n;
    int a[10][10];
    
    while (true)
    {
        cin>>n;
        
        if(n == 0)
        {
            break;
        }
        
        for (int i = 0; i <= n; i++)
        {
            for (int j = 0; j <= n; j++)
            {
                a[i][j] = 0;
            }
        }

        
        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][j] += a [i][j];
            }
        }
        
        for (int i = 0; i <= n; i++)
        {
            for (int j = 0; j <= n; j++)
            {
                cout<<setw(5)<<a[i][j];
            }
            
            cout<<endl;
        }
    }
    
}

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

C++: #include<iostream>
#include<cstring>
using namespace std;
int x[10000][10000], n;
int main() {
	while (true) {
		memset(x, 0, sizeof(x));
		cin >> n;
		if (!n) { break; }
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> x[i][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++) {
				cout << x[i][j] << endl;
			}
		}
	}
}

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

int main(){
int N,num[11][11]={},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++)
{
a =5-( (int)log10((double)(num[i][j]) )+ 1);
for(int k=0;k<a;k++)
{cout<<" ";}
cout<<num[i][j];}
cout<<endl;a=0


}

for(int i=0;i<=N;i++){
for(int j=0;j<=N;j++){
num[i][j]=0;
}
}


}

return 0;
}

C++: #include <iostream>
#include <vector>
#include <iomanip>
 
using namespace std;
 
int main(){
  int n;
  while(cin>>n&&n){
    vector<int> data((n+1)*(n+1));
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        cin>>data[i*(n+1)+j];
        data[i*(n+1)+n]+=data[i*(n+1)+j];
        data[n*(n+1)+j]+=data[i*(n+1)+j];
        data[(n+1)*(n+1)-1]+=data[i*(n+1)+j];
      }
    }
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++){
        cout<<setw(5)<<data[i*(n+1)+j];
      }
      cout<<endl;
    }
  }
}

C++: #include <cstdio>

using namespace std;

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

C++: #include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fr first
#define sc second
typedef pair<int, int> Pii;
typedef vector<int> Vi;
const int INF = (1<<30);
const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};

int main() {
  int n;
  while( cin >> n, n ) {
    int data[10][10], h[10]={0}, w[10]={0};
    int sum = 0;
    for(int i=0; i<n; i++) {
      for(int j=0; j<n; j++) {
	cin >> data[i][j];
	h[i] += data[i][j];
	w[j] += data[i][j];
	sum += data[i][j];
      }
    }

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

    for(int i=0; i<n; i++) printf("%5d", w[i]);
    printf("%5d\n", sum);
  }
}

C++: // 2011/01/05 Tazoe

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	while(true){
		int n;
		cin >> n;
		if(n==0)
			break;

		int tab[11][11];
		for(int i=0; i<n; i++)
			for(int j=0; j<n; j++)
				cin >> tab[i][j];

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

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

		for(int i=0; i<n+1; i++){
			for(int j=0; j<n+1; j++)
				cout << setw(5) << tab[i][j];
			cout << endl;
		}
	}

	return 0;
}

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

int log10(int n) {
	int keta = 0;
	while (n > 0) {
		n /= 10;
		keta++;
	}
	return keta;
}

int main() {
	const int MAX_N = 20;
	int N;
	int table[MAX_N][MAX_N];

	while (1) {
		for (int i = 0;i < MAX_N;i++) {
			for (int j = 0;j < MAX_N;j++) {
				table[i][j] = 0;
			}
		}
		scanf("%d", &N);
		if (!N)break;
		for (int i = 0;i < N;i++) {
			for (int j = 0;j < N;j++) {
				scanf("%d", &table[i][j]);
			}
		}

		for (int i = 0;i < N;i++) {
			for (int j = 0;j < N;j++) {
				table[i][N] += table[i][j];
				table[N][j] += table[i][j];
				table[N][N] += table[i][j];
			}
		}

		for (int i = 0;i <= N;i++) {
			for (int j = 0;j <= N;j++) {
				for (int k = 0;k < 5-log10(table[i][j]);k++) {
					printf(" ");
				}
				printf("%d", &table[i][j]);
			}
			printf("\n");
		}
	}

	return 0;
}