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 <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
using namespace std;

int main() {
	int n;
	while (cin >> n&&n) {
		int g[15][15];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> g[i][j];
			}
		}
		for (int i = 0; i < n; i++) {
			int sum = 0;
			for (int j = 0; j < n; j++) {
				sum += g[i][j];
			}
			g[i][n] = sum;
		}
		for (int j = 0; j < n; j++) {
			int sum = 0;
			for (int i = 0; i < n; i++) {
				sum += g[i][j];
			}
			g[n][j] = sum;
		}
		int ssum = 0;
		for (int i = 0; i < n; i++) {
			ssum += g[i][n];
		}
		g[n][n] = ssum;
		for (int i = 0; i <= n; i++) {
			for (int j = 0; j <= n; j++) {
				if (g[i][j] < 10)cout << "    " << g[i][j];
				else if (g[i][j] < 100)cout << "   " << g[i][j];
				else if (g[i][j] < 1000)cout << "  " << g[i][j];
				else if (g[i][j] < 10000)cout << " " << g[i][j];
				else cout << g[i][j];
			}
			cout << endl;
		}
	}
}

C++: #include <bits/stdc++.h>
#define MAX_N 10
using namespace std;
int table[MAX_N+1][MAX_N+1];
int n;

void print() {
    for(int r = 0; r < n + 1; r++) {
        for(int c = 0; c < n + 1; c++) {
            printf("%-5d", table[r][c]);
        }
        putchar('\n');
    }
}

int calcVirtual(int x) {
    int sum = 0;
    for(int r = 0; r < n; r++) {
        sum += table[r][x];
    }
    return(sum);
}

int calcHorizontal(int y) {
    int sum = 0;
    for(int r = 0; r < n; r++) {
        sum += table[y][r];
    }
    return(sum);
}

int main(void) {
    while(cin >> n, n != 0) {
        for(int r = 0; r < n; r++) {
            for(int c = 0; c < n; c++) {
                cin >> table[r][c];
            }
        }
        int sum = 0;
        for(int r = 0; r < n; r++) {
            sum += (table[n][r] = calcVirtual(r));
            table[r][n] = calcHorizontal(r);
        }
        table[n][n] = sum;
        print();
    }
}

C++: #include<iostream>
#include<vector>
int main(){
	while(1){
		int a;
		std::cin>>a;
		if(a==0)break;
		std::vector<std::vector<int> > vec(a+1,std::vector<int> (a+1));
		for(int i=0;i<a;i++){
			int b=0;
			for(int j=0;j<a;j++){
				std::cin>>vec[i][j];
				b+=vec[i][j];
			}
			vec[i][a]=b;
		}
		for(int i=0;i<a;i++){
			int c=0;
			for(int j=0;j<a;j++){
				c+=vec[j][i];
			}
			vec[a][i]=c;
		}
		int d=0;
		for(int i=0;i<a;i++){
			d+=vec[a][i];
		}
		vec[a][a]=d;
		for(int i=0;i<a+1;i++){
			for(int j=0;j<a+1;j++){
				if(j!=a)std::cout<<vec[i][j]<<" ";
				else std::cout<<vec[i][j];
			}
			std::cout<<std::endl;
		}
	}
	return 0;
}

C++: #include<iostream>

using namespace std;

int main()
{
	while(1)
	{
		int data[11][11] = {0};
		int n;
		cin >> n;
		if(n == 0) break;
		for(int y = 0;y <= n;y++)
		{
			for(int x = 0;x <= n;x++)
			{
				if(y < n && x < n) cin >> data[y][x];
				if(x < n) data[y][n] += data[y][x];
				if(y < n) data[n][x] += data[y][x];
				cout.width(4);
				if(x < n || y < n) cout << data[y][x];
				else cout << data[y][x] / 2;
				if(x < n) cout << ' ';
			}
			cout << endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
	int n,s[11];
	while(cin>>n,n){
		fill(s,s+10,0);int l=0;
		for(int i=0;i<n;i++){
			int y=0,t;
			for(int j=0;j<n;j++)cin>>t,y+=t,s[j]+=t,printf("%5d",t);
			cout<<y<<endl;
		}
		for(int i=0;i<n;i++)printf("%5d",s[i]),l+=s[i];
		cout<<l<<endl;
	}
}

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

int main(void)
{
	int   n;
	int** ppMat;

	while(1)
	{
		cin >> n;
		if(n == 0)break;
	
		ppMat = new int*[n+1];
		for(int i=0; i<n+1; i++)
		{
			ppMat[i] = new int[n+1];
			for(int j=0; j<n+1; j++)
				ppMat[i][j] = 0;
		}

		for(int y=0; y<n; y++)
		for(int x=0; x<n; x++)
		{
			cin >> ppMat[x][y];
			ppMat[x][n] += ppMat[x][y];
			ppMat[n][y] += ppMat[x][y];
			ppMat[n][n] += ppMat[x][y];
		}

		for(int y=0; y<n+1; y++)
		{
			for(int x=0; x<n+1; x++)
			{
				cout.width(5);
				cout << ppMat[x][y];
			}
			cout << endl;
		}
		
		for(int i=0; i<n; i++)
			delete [](ppMat[i]);
	}


	return 0;
}

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=1;k<a;k++)
{cout<<" ";}
cout<<num[i][j];}
cout<<endl;


}

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


}

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));

		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;
		}
	}
}