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 <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

const double EPS = 1e-10;
const double PI  = acos(-1.0);

#define CLR(a) memset((a), 0 ,sizeof(a))

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

int dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[8] = {0, -1, 0, 1, -1, 1, -1, 1};

int main() {
  int n, field[11][11];  
  
  while(1) {
    cin >> n;
    if(!n) break;

    REP(i, n) {
      REP(j, n) {
	cin >> field[i][j];
      }
    }

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

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

    field[n][n] = 0;
    REP(i, n) field[n][n] += field[n-i-1][n];

    REP(i, n+1) {
      REP(j, n+1) {
	printf("%5d", field[i][j]);
      }
      printf("\n");
    }
  }
}

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

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long ll;

#define REP(i, m, n) for (int i = static_cast<int>(m); i < static_cast<int>(n); ++i)

int main()
{
    
    int n;
    
    VVI vvi;
    int sum;

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

        REP(i, 0, n) {
            vvi.push_back(vector<int>());
            sum = 0;
            
            REP(j, 0, n) {
                int in;
                std::cin >> in;
                vvi[i].push_back(in);
                sum += in;
            }
            vvi[i].push_back(sum);
        }

        REP(i, 0, n + 1) {
            int sum = 0;
            
            REP(j, 0, n + 1) {
                sum += vvi[j][i];
            }
            
            vvi[n].push_back(sum);
        }
        
        REP(i, 0, vvi.size()) {
            REP(j, 0, vvi[i].size()) {
                std::cout << std::setw(5) << vvi[i][j];
            }
            std::cout << std::endl;
        }
    }
    return 0;
}

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, 0){
    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++){
        if(j) cout << " ";
        cout << mt[i][j];
      }
      cout << endl;
    }
  }
  return 0;
}

C++: #include <stdio.h>
#include <iostream>

using namespace std;

int main() {
	int hyou[10][10];
	int sum;
	int n;

	while (cin >> n) {
		if (n == 0) {
			break;
		}

		int s[10] = {0};

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

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

	return 0;


}

C++: #include "bits/stdc++.h"
using namespace std;

int main(){
	int n;
	while(1){
	cin>>n;
		if(n==0)break;
	vector<vector<int> > a(n+1,vector<int>(n+1));
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>a[i][j];
		}
	}
	cout<<" ";
	for(int i=0;i<n;i++){
		int b=0;
		
		for(int j=0;j<n;j++){
			cout<<"  "<<a[i][j]<<" ";
			b+=a[i][j];
		}
		cout<<" "<<b<<endl<<" ";
	}
	
	int d=0;
	
	for(int i=0;i<n;i++){
		int c=0;
		for(int j=0;j<n;j++){
			c+=a[j][i];
		}
		cout<<" "<<c<<" ";
		d+=c;
	}
	cout<<" "<<d<<endl;
	}
	return 0;
}

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

int main() {
	int n;
	while (cin >> n, n) {
		int excel[11][11] = {};

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

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

		for (int i = 0;i < n;i++) {
			for (int j = 0;j < n;j++) {
				excel[n][i] += excel[j][i];
			}
		}
		for (int i = 0;i <= n;i++) {
			for (int j = 0;j <= n;j++) {
				printf("%5d", excel[i][j]);
				if (j == n) { cout << endl; }
			}
		}
	}
	return 0;
}

C++: #include<stdio.h>

int main(){
  while(1){
    int i,j,n;
    int a[10][10];
    scanf("%d", &n);
    if(n!=0){
      for(i=0;i<n;i++){
	for(j=0;j<n;j++){
	  scanf("%d", &a[i][j]);
	}
      }

      for(i=0;i<n;i++){
	int m=0;
	for(j=0;j<n;j++){
	  m+=a[i][j];
	  printf("%5d", a[i][j]);
	}
	printf("%5d\n", m);
      }
      int k=0;
      for(j=0;j<n;j++){
	int l=0;
	for(i=0;i<n;i++){
	  l+=a[i][j];
	}
	printf("%5d", l);
	k+=l;
      }
      printf("%5d\n", k);
    }

    else
      break;
  }
  return 0;
}