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<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
using namespace std;
int main(void)
{
	int i,j,hyo[12][12],n,wa[4],wa2[4],sum;
	while(1) {
		scanf("%d",&n);
		if(n==0)  break;
		for(i=0;i<n;i++) {
			for(j=0;j<n;j++) scanf("%d",&hyo[i][j]);
		}
		for(i=0;i<n;i++) {
			sum=0;
			for(j=0;j<n;j++) sum+=hyo[i][j];
			wa[i]=sum;
		}
		for(i=0;i<n;i++) {
			sum=0;
			for(j=0;j<n;j++) sum+=hyo[j][i];
			wa2[i]=sum;
		}
		for(i=0;i<n;i++) {
			for(j=0;j<n;j++) {
				if(hyo[i][j]==100) printf(" %d ",hyo[i][j]);
				if(hyo[i][j]<100 && hyo[i][j]>=10) printf("  %d ",hyo[i][j]);
			}
			printf("%d\n",wa[i]);
		}
		for(i=0;i<4;i++) {
			if(wa2[i]>=1000) printf("%d ",wa2[i]);
			if(wa2[i]>=100 && wa2[i]<1000) printf(" %d ",wa2[i]);
			else printf("   %d ",wa2[i]);
		}
		sum=0;
		for(i=0;i<4;i++) {
			sum+=wa[i];
		}
		if(sum==1000) printf("%d\n",sum);
		if(sum<1000 && sum>=10) printf("%d\n",sum);
	}
	return 0;
}

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

int main(){
	while(1){
		int n;	
		int a[12][12];
		
		scanf(" %d", &n);
		if(n==0) break;
		
		a[n][n]=0;
		for(int i=0; i<n; ++i){
			a[i][n]=0;
			for(int j=0; j<n; ++j){
				scanf(" %d", &a[i][j]);
				a[i][n]+=a[i][j];
			}
			a[n][n]+=a[i][n];
		}
		
		for(int i=0; i<n; ++i){
			a[n][i]=0;
			for(int j=0; j<n; ++j){
				a[n][i]+=a[j][i];
			}
		}
		
		for(int i=0; i<=n; ++i){ 
			for(int j=0; j<=n; ++j){
				printf(" %d", a[i][j]);
			}
			printf("\n");
		}
				
	}
	return 0;
}

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;


}