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 <fstream>
#include <sstream>
#include <iomanip> 
#include <complex> 
#include <string>
#include <vector> 
#include <list>
#include <deque> 
#include <stack> 
#include <queue> 
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm> 
#include <numeric> 
#include <typeinfo> 
#include <cstdio>
#include <cstdlib> 
#include <cstring>
#include <cmath>
#include <climits> 
#include <ctime> 

using namespace std;

int main(void){
	int n;
	int a,b;
	int hyo[10][10];
	while(1){
		cin >> n;
		if(n==0)break;
		for(b=0;b<=n;b++){
			for(a=0;a<=n;a++){
				hyo[a][b]=0;
			}
		}
		for(b=0;b<n;b++){
			for(a=0;a<n;a++){
				cin >> hyo[a][b];
				hyo[n][b]+=hyo[a][b];
			}
		}
		for(b=0;b<=n;b++){
			for(a=0;a<n;a++){
				hyo[b][n]+=hyo[b][a];
			}
		}
		for(b=0;b<=n;b++){
			for(a=0;a<=n;a++){
				cout << hyo[a][b] << ' ';
			}
			cout << endl;
		}
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

int main(int argc, char const* argv[])
{
	int n;
	int data[12][12];

	while( cin >> n && n != 0 ){
		for( int i = 0;i < n;i++ ){
			for( int j = 0;j < n;j++ ){
				cin >> data[i][j];
			}
		}

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

		for( int i = 0;i < n + 1;i++ ){
			for( int j = 0;j < n + 1;j++ ){
				cout.width( 5 );
				cout << data[i][j];
			}
			cout << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <iomanip>
#include <vector>

using namespace std;


int n;
int table[11][11];
int row_sum;
int col_sum;


int main()
{
	while (cin >> n)
	{
		if (n == 0) break;
		
		for (int i=0; i<n; i++)
		{
			row_sum = 0;
			for (int j=0; j<n; j++)
			{
				cin >> table[i][j];
				row_sum += table[i][j];
			}
			table[i][n] = row_sum;
		}
		
		for (int i=0; i<=n; i++)
		{
			col_sum = 0;
			for (int j=0; j<n; j++)
			{
				col_sum += table[j][i];
			}
			table[n][i] = col_sum;
		}
		
		for (int i=0; i<=n; i++)
		{
			for (int j=0; j<=n; j++)
			{
				printf("%5d", table[i][j]);
			}
			cout << endl;
		}
	}
	
	return 0;
}

C++: //Name: Matrix-like Computation
//Level: 1
//Category: やるだけ
//Note:

#include <iostream>
#include <vector>

using namespace std;

bool solve() {
    int N;
    if(!(cin >> N)) return false;
    if(!N) return false;

    vector<vector<int>> v(N+1, vector<int>(N+1, 0));
    for(int i = 0; i < N; ++i) {
        for(int j = 0; j < N; ++j) {
            cin >> v[i][j];
            v[i][N] += v[i][j];
            v[N][j] += v[i][j];
            v[N][N] += v[i][j];
        }
    }
    for(int i = 0; i < N+1; ++i) {
        for(int j = 0; j < N+1; ++j) {
            cout.width(5);
            cout << v[i][j];
        }
        cout << endl;
    }
    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);

    while(solve()) ;
    return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int n;
	int i,j;
	int a[10+1][10+1];
	
	scanf("%d",&n);
	
	while(n!=0){
		
		for (i=0; i<=n; i++) 
			for (j=0; j<=n; j++)
				a[i][j]=0;
		
		for(i=0;i<n;i++){
			for(j=0;j<n;j++){
				scanf("%d",&a[i][j]);
			}
		}
		for (i=0; i<n; i++) 
			for (j=0; j<n; j++) 
				a[i][n]+=a[i][j];
		
		for (i=0; i<n; i++) 
			for (j=0; j<n; j++) 
				a[n][j]+=a[i][j];
		
		for (i=0; i<n; i++) 
			a[n][n]+=a[i][n];
		
		for (i=0; i<=n; i++) {
			for (j=0; j<=n; j++) 
				printf ("%5d",a[i][j]);
			printf ("\n");
		}
		scanf("%d",&n);
	}
	return 0;
}

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

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

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

	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;
		if (!first)printf("\n");
		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]);
			}
			if(i < N) printf("\n");
		}
		first = false;
	}

	return 0;
}

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

int hoge(int w);
int getKetasu(int n);

int main(){
	int w;
	
	while(1){
	 cin>>w;
	 if(w==0){break;}
	 hoge(w);
	}
	
	return 0;
}

int hoge(int w){	//?????£???????????????
	int t[10][10];
	int ketasu;
	int x,y,i;
	
	for(y=0;y<w;y++){
	 for(x=0;x<w;x++){
	  cin>>t[x][y];
	 }
	}
	
	for(y=0;y<w;y++){
	 t[w][y]=0;
	 for(x=0;x<w;x++){
	  t[w][y]+=t[x][y];
	 }
	}
	for(x=0;x<=w;x++){
	 t[x][w]=0;
	 for(y=0;y<w;y++){
	  t[x][w]+=t[x][y];
	 }
	}
	
	for(y=0;y<=w;y++){
	 for(x=0;x<=w;x++){
	  cout<<" ";
	  ketasu=5;
	  for(i=getKetasu(t[x][y]);i<ketasu;i++){cout<<" ";}
	  cout<<t[x][y];
	 }
	 cout<<endl;
	}
	
	return 0;
}

int getKetasu(int n){	//?????°?????????
	if(n<10){
	 return 1;
	}else if(n<100){
	 return 2;
	}else if(n<1000){
	 return 3;
	}else{
	 return 4;
	}
}

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

using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int n;
  while (cin >> n, n) {
    int a[15][15] = {};
    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++) {
        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];
      }
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        cout << setw(5) << a[i][j];
      }
      cout << endl;
    }
  }

  return 0;
}

C++: #include <cstdio>
#include <iostream>
typedef long long ll;
#define rep2(x,from,to) for(int x=(from);(x)<(to);(x)++)
#define rep(x,to) rep2(x,0,to)

using namespace std;

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