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>

int map[11][11];

int main(void)
{
    while(true) {
        int n;

        scanf("%d", &n);
        if(!n) break;

        memset(map, 0, sizeof(map));

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

        for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
            map[i][n] += map[i][j];
        for(int j=0; j<=n; j++)
        for(int i=0; i<n; i++)
            map[n][j] += map[i][j];

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

    }
    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()
{
	int n;
	while(cin >> n && n)
	{
		vector<vint> v(n + 1, vint(n + 1, 0));

		rep(y, n) rep(x, n)
			cin >> v[y][x];


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

		v[n][n] = sum;


		rep(y, n+1)	rep(x, n+1)
			printf("%5d%s", v[y][x], (n == x ? "\n" : ""));		
	}
}

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

int main(){
	int n;
	while(std::cin>>n,n){
		int data[11][11];
		memset(data,0,sizeof(data));
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				std::cin>>data[i][j];
				data[i][n]+=data[i][j];
				data[n][j]+=data[i][j];
				data[n][n]+=data[i][j];
			}
		}
		for(int i=0;i<n+1;i++){
			for(int j=0;j<n+1;j++){
				std::cout<<std::setw(5)<<std::right<<data[i][j];
			}
			std::cout<<std::endl;
		}
	}
	return 0;
}

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

int main(){
	int n;
	while(cin >> n){
		if(n == 0) break;
		else{
			int data[n+2][n+2];

			for (int i = 0; i < n+2; ++i){
				for(int j = 0; j < n+2; j++){					
				data[i][j] = 0;
				}
			}
			
			for (int i = 0; i < n; ++i){
				for(int j = 0; j < n; j++){					
				cin >> data[i][j];
				}
			}
			for (int i = 0; i < n; ++i){
				for(int j = 0; j < n; j++){	
					data[i][n] += data[i][j];
				}
			}
			for (int i = 0; i < n+1; ++i){
				for(int j = 0; j < n; j++){	
					data[n][i] += data[j][i];
				}
			}
			for (int i = 0; i < n+1; ++i){
				for(int j = 0; j < n+1; j++){
					cout<<" "<< data[i][j];
					// if (j != n) cout<<" ";
				}
				cout << endl;
			}
				
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <math.h>
#include <map>
#include <queue>
#include <string>
using namespace std;

int n,d[11][11];

int main(){
	while(cin>>n){
		if(n==0)return 0;
		for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>d[i][j];
		int ss=0;
		for(int i=0;i<n;i++){
			int s=0;
			for(int j=0;j<n;j++)s+=d[i][j];
			d[i][n]=s;
			ss+=s;
			s=0;
			for(int j=0;j<n;j++)s+=d[j][i];
			d[n][i]=s;
		}
		d[n][n]=ss;
		for(int i=0;i<=n;i++){
			for(int j=0;j<=n;j++){
				int m=1,p=10;
				while(d[i][j]>=p){m++;p*=10;}
				for(int k=0;k<5-m;k++)cout<<" ";
				cout<<d[i][j];
			}
			cout<<endl;
		}
	}
}

C++: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int n;
    while(cin >> n, n){
        vector<vector<int>> v(n+1, vector<int>(n+1));
        for (int i = 0; i < n; ++i) {
            int s = 0;
            for (int j = 0; j < n; ++j) {
                cin >> v[i][j];
                s += v[i][j];
            }
            v[i].back() = s;
        }
        for (int i = 0; i < n+1; ++i) {
            int s = 0;
            for (int j = 0; j < n; ++j) {
                s += v[j][i];
            }
            v.back()[i] = s;
        }
        for (int i = 0; i <= n; ++i) {
            for (int j = 0; j <= n; ++j) {
                printf("%5d", v[i][j]);
            }
            puts("");
        }
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main (int argc, char const* argv[]) {
  int ar[11][11], N, get, sum;
  while (true) {
    // load
    cin >> N;
    if (N == 0) break;
    memset(ar, 0, sizeof ar);
    for (int i = 0; i < N; i += 1) {
      for (int j = 0; j < N; j += 1) {
        cin >> get;
        ar[i][j] = get;
      }
    }
    // calc
    for (int i = 0; i < N; i += 1) {
      int sum_h = 0, sum_v = 0;
      for (int j = 0; j < N; j += 1) {
        sum_h += ar[i][j];
        sum_v += ar[j][i];
      }
      ar[i][N] = sum_h;
      ar[N][i] = sum_v;
    }
    // sum
    sum = 0;
    for (int i=0;i<N;i++) sum += ar[i][N];
    ar[N][N] = sum;
    // print
    for (int i = 0; i <= N; i += 1) {
      for (int j = 0; j <= N; j += 1) {
        printf("%5d",ar[i][j]);
      }
      cout<<endl;
    }
  }
  return 0;
}

C++: #include<cstdio>
int main()
{
    int n,hyo[11][11];
    while(scanf("%d",&n),n)
    {
        for(int i=0;i<n+1;i++)
        {
            hyo[n][i]=0;
            hyo[i][n]=0;
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                scanf("%d",&hyo[i][j]);
                hyo[n][i]+=hyo[i][j];
                hyo[i][n]+=hyo[i][j];
            }
        }
        for(int i=0;i<n;i++)hyo[n][n]+=hyo[i][n];
        for(int i=0;i<n+1;i++)for(int j=0;j<n+1;j++)
        {
            if(hyo[i][j]%10==hyo[i][j])printf("    ");
            else if(hyo[i][j]%100==hyo[i][j])printf("   ");
            else if(hyo[i][j]%1000==hyo[i][j])printf("  ");
            else if(hyo[i][j]%10000==hyo[i][j])printf(" ");
            printf("%d",hyo[i][j]);
            if(j==n)printf("\n");
        }
    }
    return 0;
}