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 <sstream>
#include <vector>
#include <string>

#include <typeinfo>

using namespace std;

//split numbers
vector<int> split(const string &s,char c){
	vector<int> elems;
	string a;
	stringstream ss(s);
	while (getline(ss, a, c)){
		if (!a.empty()){
			elems.push_back(stoi(a));
		}
	}
	return elems;
}

int main(){
	int n,all=0;
	int data[256];
	int tdata[256] = { 0 };
	
	cin >> n;

	for (int i = 0; i < n; i++,all=0){
		for (int j = 0; j < n; j++){
			cin >> data[j];
			printf(" %d",data[j]);
			all += data[j];
			tdata[j] += data[j];
		}
		printf(" %d\n", all);
	}

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

	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,x,n) for(int i=(x);i<(n);i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define ub upper_bound
#define lb lower_bound
#define int long long
using namespace std;
const int MOD=1000000007;
const int INF=1000000009;


signed main(){
  int n;
  int M[12][12];
  while(cin>>n,n){
    rep(i,n)rep(j,n)cin>>M[i][j];
    rep(i,n){
      int tmp=0;
      rep(j,n)tmp+=M[i][j];
      M[i][n]=tmp;
    }
    rep(i,n){
      int tmp=0;
      rep(j,n)tmp+=M[j][i];
      M[n][i]=tmp;
    }
    int tmp=0;
    rep(i,n)tmp+=M[n][i];
    M[n][n]=tmp;
    rep(i,n+1){
      rep(j,n+1)cout<<setw(5)<<M[i][j];
      cout<<endl;
    }
  }
  return 0;
}

C++: #include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<cstdlib>
#include<iomanip>
#include<queue>


using namespace std;

void Solution()
{
    int n;
    int a[10][10];
    
    while (true)
    {
        cin>>n;
        
        if(n == 0)
        {
            break;
        }
        
        for (int i = 0; i <= n; i++)
        {
            for (int j = 0; j <= n; j++)
            {
                a[i][j] = 0;
            }
        }

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

int main()
{
    Solution();
    return 0;
}

C++: #include<iostream>
#include<cstring>
using namespace std;
int x[10000][10000], n;
int main() {
	while (true) {
		memset(x, 0, sizeof(x));
		cin >> n;
		if (!n) { break; }
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> x[i][j]; x[i][n] += x[i][j]; x[n][j] += x[i][j]; x[n][n] += x[i][j];
			}
		}
		for (int i = 0; i <= n; i++) {
			for (int j = 0; j <= n; j++) {
				cout << x[i][j] << endl;
			}
		}
	}
}

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


}

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


}

return 0;
}

C++: #include <iostream>
#include <vector>
#include <iomanip>
 
using namespace std;
 
int main(){
  int n;
  while(cin>>n&&n){
    vector<int> data((n+1)*(n+1));
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        cin>>data[i*(n+1)+j];
        data[i*(n+1)+n]+=data[i*(n+1)+j];
        data[n*(n+1)+j]+=data[i*(n+1)+j];
        data[(n+1)*(n+1)-1]+=data[i*(n+1)+j];
      }
    }
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++){
        cout<<setw(5)<<data[i*(n+1)+j];
      }
      cout<<endl;
    }
  }
}

C++: #include <cstdio>

using namespace std;

int main(void){
	int t[11][11], n, i, j, sum;
	while(1){
		scanf("%d",&n);
		if(!n) break;
		for(i = 0;i < n;i++){
			for(j = 0;j < n;j++){
				scanf("%d",&t[i][j]);
			}
		}
		for(i = 0;i < n;i++){
			sum = 0;
			for(j = 0;j < n;j++){
				sum += t[i][j];
			}
			t[i][j] = sum;
			sum = 0;
			for(j = 0;j < n;j++){
				sum += t[j][i];
			}
			t[j][i] = sum;
		}
		sum = 0;
		for(j = 0;j < n;j++){
			sum += t[i][j];
		}
		t[i][i] = sum;
		for(i = 0;i < n + 1;i++){
			for(j = 0;j < n + 1;j++){
				printf("%5d",t[i][j]);
			}
			printf("\n");
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fr first
#define sc second
typedef pair<int, int> Pii;
typedef vector<int> Vi;
const int INF = (1<<30);
const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};

int main() {
  int n;
  while( cin >> n, n ) {
    int data[10][10], h[10]={0}, w[10]={0};
    int sum = 0;
    for(int i=0; i<n; i++) {
      for(int j=0; j<n; j++) {
	cin >> data[i][j];
	h[i] += data[i][j];
	w[j] += data[i][j];
	sum += data[i][j];
      }
    }

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

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

C++: // 2011/01/05 Tazoe

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	while(true){
		int n;
		cin >> n;
		if(n==0)
			break;

		int tab[11][11];
		for(int i=0; i<n; i++)
			for(int j=0; j<n; j++)
				cin >> tab[i][j];

		for(int i=0; i<n; i++){
			tab[i][n] = 0;
			for(int j=0; j<n; j++)
				tab[i][n] += tab[i][j];
		}

		for(int j=0; j<n+1; j++){
			tab[n][j] = 0;
			for(int i=0; i<n; i++)
				tab[n][j] += tab[i][j];
		}

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

	return 0;
}

C++: #include <stdio.h>
//#include <math.h>
using namespace std;

int log10(int n) {
	int keta = 0;
	while (n > 0) {
		n /= 10;
		keta++;
	}
	return keta;
}

int main() {
	const int MAX_N = 20;
	int N;
	int table[MAX_N][MAX_N];

	while (1) {
		for (int i = 0;i < MAX_N;i++) {
			for (int j = 0;j < MAX_N;j++) {
				table[i][j] = 0;
			}
		}
		scanf("%d", &N);
		if (!N)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++) {
			for (int j = 0;j < N;j++) {
				table[i][N] += table[i][j];
				table[N][j] += table[i][j];
				table[N][N] += table[i][j];
			}
		}

		for (int i = 0;i <= N;i++) {
			for (int j = 0;j <= N;j++) {
				for (int k = 0;k < 5-log10(table[i][j]);k++) {
					printf(" ");
				}
				printf("%d", &table[i][j]);
			}
			printf("\n");
		}
	}

	return 0;
}