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 <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <climits>
#include <cfloat>

using namespace std;


int table[4096][4096];
int main()
{
	while (true)
	{
		int n;
		scanf("%d", &n);
		if (n == 0)
			break;

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

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

	return 0;
}

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

int main(){
	int data[11][11];
	int n;
	cin >> n;
	while (n){

		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				cin >> data[i][j];
			}
		}
		int sum_col[10] = { 0 };
		int sum_low[10] = { 0 };

		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				sum_col[i] += data[i][j];
				sum_low[i] += data[j][i];
			}
			data[i][n] = sum_col[i];
			data[n][i] = sum_low[i];
		}
		int sum = 0;
		for (int i = 0; i < n; i++){
			sum += data[i][n];
		}
		data[n][n] = sum;
		for (int i = 0; i < n + 1; i++){
			for (int j = 0; j < n + 1; j++){
				printf("%5d", data[i][j]);
			}
			if (i != n)	printf("\n");
		}
		cin >> n;
		if (n) printf("\n");
	}
	
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;
 
int main()
{
    int n;
    while(cin>>n){
	if(n==0) return 0;
         int a[11][11]={0};
	 int sum=0;
         for(int i=0;i<n;i++){
             for(int j=0;j<n;j++){
		cin>>a[i][j]; 
                sum+= a[i][j]; 
             }
          }

          for(int i=0;i<n;i++){
             for(int j=0;j<n;j++){
               a[i][n]+=a[i][j];                
             }
          }
          for(int j=0;j<n;j++){
             for(int i=0;i<n;i++){
                a[n][j]+=a[i][j];               
             }
          }
          a[n][n]=sum;
          for(int i=0;i<n+1;i++){
             for(int j=0;j<n+1;j++){
		cout<<setw(5)<<a[i][j];
             }
             cout<<endl;
           }
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
#define rer(i,l,n) for(int i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int,int> pii;

int main(){
	int n;
	while(1){
		cin>>n;
		if(n==0) break;
		int d[12][12]={};
		rep(i,0,n) rep(j,0,n){
			cin>>d[i][j]; d[i][n]+=d[i][j];
		}
		rer(i,0,n) rep(j,0,n) d[n][i]+=d[j][i];
		rer(i,0,n){
			rer(j,0,n){
				printf("%5d",d[i][j]);
			}
			printf("\n");
		}
	}
}

C++: #include <iostream>
#include <fstream>
#include <typeinfo>
#include <vector>
#include <cmath>
#include <map>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const double pi=acos(-1);
const double eps=1e-8;

int n;

int main(){
	while(1){
		cin>>n;
		if(!n) break;
		vvi ans(n+1,vi(n+1));
		for(int i=0;i<n;i++){
			int tmp=0;
			for(int j=0;j<n;j++){
				cin>>ans[i][j];
				tmp+=ans[i][j];
			}
			ans[i][n]=tmp;
		}
		for(int i=0;i<=n;i++){
			int tmp=0;
			for(int j=0;j<n;j++){
				tmp+=ans[j][i];
			}
			ans[n][i]=tmp;
		}
		for(int i=0;i<=n;i++){
			for(int j=0;j<=n;j++){
				cout.width(5);
				cout<<ans[i][j];
			}
			cout<<endl;
		}
	}
}

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

C++: #include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int a[15][15];
int main() {
	int n;
	while (cin >> n, n) {
		rep(i, n) {
			rep(j, n) {
				scanf("%d", &a[i][j]);
			}
		}
		rep(i, n) {
			int cnt = 0;
			rep(j, n)cnt += a[i][j];
			a[i][n] = cnt;
		}
		rep(i, n) {
			int cnt = 0;
			rep(j, n)cnt += a[j][i];
			a[n][i] = cnt;
		}
		a[n][n] = 0;
		rep(i, n)a[n][n] += a[i][n];
		rep(i, n + 1) {
			rep(j, n + 1) {
				printf("%5d", a[i][j]);
				if (j == n)printf("\n");
			}
		}
	}
	return 0;
}

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

int main(){
  int n;
  while(cin>>n){
    if(n==0)break;
    int a[11][11],b[10]={},c[10]={},d=0;
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
	cin>>a[i][j];
    
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++){
	b[i]+=a[i][j];
	c[i]+=a[j][i];
      }
    for(int i=0;i<n;i++)
      d+=b[i];
    for(int i=0;i<n+1;i++){
      a[i][n]=b[i];
      a[n][i]=c[i];
      a[n][n]=d;
    }
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++)
	printf("%5d",a[i][j]);
      cout<<endl;
    }
  }
  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;
}