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>
using namespace std;
const int N=10000;
int n,a[N][N];
signed main(void){
	ios::sync_with_stdio(false);
	while(cin>>n&&n){
		memset(a,0,sizeof(a));
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				cin>>a[i][j];
		for(int i=1,sum;i<=n;i++){
			sum=0;
			for(int j=1;j<=n;j++)
				sum+=a[i][j];
			a[i][n+1]=sum;
		}
		for(int j=1,sum;j<=n;j++){
			sum=0;
			for(int i=1;i<=n;i++)
				sum+=a[i][j];
			a[n+1][j]=sum;
		}
		int sum=0;
		for(int i=1;i<=n;i++)
			 sum+=a[i][n+1];
		a[n+1][n+1]=sum;
		for(int i=1;i<=n+1;i++,cout<<endl)
			for(int j=1;j<=n+1;j++)
				cout<<setw(5)<<a[i][j];
	}
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int main(){
  int n;
  while(cin >>n,n){
    int mat[11][11] = {0};
    for(int i=0; i<n; i++){
      for(int j=0; j<n; j++){
	cin >>mat[i][j];
      }
    }
    int k = 0;
    for(int i=0; i<n; i++){
      int s = 0, m = 0;
      for(int j=0; j<n; j++){
	s+=mat[i][j];
	m+=mat[j][i];
      }
      mat[i][n] = s;
      mat[n][i] = m;
      k+=s;
    }
    mat[n][n] = k;
    for(int i=0; i<=n; i++){
      for(int j=0; j<=n; j++){
	printf("%5d",mat[i][j]);
      }
      cout <<endl;
    }
  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

int main()
{
	clock_t start = clock();
	int n;
	while(cin >> n && n && start + 1000 < clock())
	{
		vector<vint> v(n + 1, vint(n + 1));

		rep(i, n) rep(j, n)
			cin >> v[i][j];

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

		rep(i, n+1) 
		{
			rep(j, n+1)
			{
				rep(k, 4 - log10((float)v[i][j]))
					cout << " ";
				cout << v[i][j];
			}
			cout << endl;
		}
	}
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;



int main(){

	int n,tmp,row_sum,col_sum[11];

	while(true){
		scanf("%d",&n);
		if(n == 0)break;

		for(int i=0; i < 11; i++)col_sum[i] = 0;

		for(int i=0; i < n; i++){
			row_sum = 0;
			for(int k=0; k < n; k++){
				scanf("%d",&tmp);
				printf("%5d",tmp);
				row_sum += tmp;
				col_sum[k] += tmp;
			}
			printf("%5d\n",row_sum);
			col_sum[n] += row_sum;
		}
		for(int i=0; i <= n; i++)printf("%5d",col_sum[i]);
		printf("\n");
	}

    return 0;
}

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

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

int main()
{
  int n;
  while (cin >> n){
    int table[n][n], x[n] = {0}, y[n] = {0}, sum = 0;
    REP(i, n){
      REP(j, n) cin >> table[i][j];
    }
    REP(i, n){
      cout << setw(4) << right << table[i][0];
      x[i] = table[i][0];
      FOR(j, 1, n){
        cout << setw(4) << right << table[i][j];
        x[i] += table[i][j];
      }
      cout << setw(4) << right << x[i] << endl;
    }
    REP(i, n){
      REP(j, n) y[i] += table[j][i];
    }
    cout << setw(4) << right << y[0];
    FOR(i, 1, n) cout << setw(4) << right << y[i];
    REP(i, n) sum += x[i];
    cout << setw(4) << right << sum << endl;
  }
}

C++: #include <bits/stdc++.h>

using namespace std;

#define MAX 11
 
int main()
{
    int n, mat[MAX][MAX];
     
    while (cin >> n, n) {
        for (int i = 0; i < MAX; i++) {
            for (int j = 0; j < MAX; j++) {
                mat[i][j] = 0;
	    }
	}
         
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cin >> mat[i][j];
	    }
	}
         
        int sum = 0;
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= n; j++) {
                sum += mat[i][j];
                if (j == n) {
                    mat[i][j] = sum;
                    sum = 0;
                }
                 
            }
        }
         
        for (int j = 0; j <= n; j++) {
            for (int i = 0; i <= n; i++) {
                sum += mat[i][j];
                if (i == n) {
                    mat[i][j] = sum;
                    sum = 0;
                }
            }
        }
         
        int e = 0;
        for (int i = 0; i < n; i++) {
            e += mat[n][i];
            if (i == n-1) mat[n][n] = e;
        }
         
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= n; j++) {
		printf("%5d", mat[i][j]);
            }
            cout << endl;
        }
    }
    return 0;
}

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

using namespace std;

int main(){
  while(1){
    int n;
    char line[100];
    fgets(line,sizeof(line),stdin);
    sscanf(line,"%d",&n);
    if(n==0)return 0;
    int list[11][11];
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++){
	list[i][j]=0;
      }
    }
    for(int i=0;i<n;i++){
      //  char buf[100];
      // fgets(buf,sizeof(buf),stdin);
      for(int j=0;j<n;j++){
	//sscanf(buf,"%d",&list[i][j]);
	cin>>list[i][j];
      }
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	list[i][n]+=list[i][j];
	list[n][i]+=list[j][i];
      }
    }
    for(int i=0;i<n;i++){
      list[n][n]+=list[n][i];
    }
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++){
	printf("%5d",list[i][j]);
      }
      printf("\n");
    }
  }
}

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

void process_table(std::istream& in, std::ostream& out) {
    int n;
    in >> n;
    if (n <= 0)
        return;
    std::vector<int> col_sums(n, 0);
    for (int i = 0; i < n; ++i) {
        int row_sum = 0;
        for (int j = 0; j < n; ++j) {
            int x;
            in >> x;
            col_sums[j] += x;
            row_sum += x;
            out << std::setw(5) << x;
        }
        out << std::setw(5) << row_sum << '\n';
    }
    int sum = 0;
    for (auto const& col_sum: col_sums) {
        out << std::setw(5) << col_sum;
        sum += col_sum;
    }
    out << std::setw(5) << sum << '\n';
}

int main() {
    while (std::cin) {
        process_table(std::cin, std::cout);
    }
}