Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	stdin := bufio.NewScanner(os.Stdin)
	for stdin.Scan() {
		n, _ := strconv.Atoi(stdin.Text())
		if n == 0 {
			break
		}

		a := make([][]int, n+1)
		for i := 0; i < n; i++ {
			sum := 0
			stdin.Scan()
			a[i] = make([]int, n+1)
			str := strings.Split(stdin.Text(), " ")
			for j := 0; j < n; j++ {
				a[i][j], _ = strconv.Atoi(str[j])
				sum += a[i][j]
				fmt.Printf("%5d", a[i][j])
			}
			a[i][n] = sum
			fmt.Printf("%5d\n", a[i][n])
		}
		a[n] = make([]int, n+1)
		for i := 0; i <= n; i++ {
			sum := 0
			for j := 0; j < n; j++ {
				sum += a[j][i]
			}
			a[n][i] = sum
			fmt.Printf("%5d", a[n][i])
		}
		fmt.Println()
	}
}

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

C++: #include <iostream>
#include <stdio.h>

using namespace std;

int main(){
	int n;
	int x[12][12];
	
	while (cin >> n){
		for (int i = 1; i <= n; i++){
			for (int j = 1; j <= n; j++){
				cin >> x[i][j];
			}
		}
	}
	
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= n; j++){
			x[i][n + 1] += x[i][j];
		}
	}
	for (int j = 1; j <= n; j++){
		for (int i = 1; i <= n; i++){
			x[n + 1][j] += x[i][j];
		}
	}
	for (int i = 1; i <= n + 1; i++){
		for (int j = 1; j <= n + 1; j++){
			printf("%5d\n", x[i][j]);
		}
	}
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
    int N;
    while(cin>>N,N){
        int num[150][150]={{0}};
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                cin>>num[i][j];
                num[i][N]+=num[i][j];
                num[N][j]+=num[i][j];
                num[N][N]+=num[i][j];
            }
        }
        for(int i=0;i<=N;i++){
            for(int j=1;j<=N;j++){
                if(num[i][j]<10){
                    for(int k=0;k<4;k++)cout<<" ";
                }
                else if(num[i][j]<100){
                    for(int k=0;k<3;k++)cout<<" ";
                }
                else if(num[i][j]<1000){
                    for(int k=0;k<2;k++)cout<<" ";
                }
                else if(num[i][j]<10000){
                    for(int k=0;k<1;k++)cout<<" ";
                }
                cout<<num[i][j];
            }
            cout<<endl;
        }
    }
    return 0;
}

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

int main(){
	int data_size;
	cin >> data_size;
	while (data_size){
		vector<vector<int>> data;
		vector<vector<int>> data_digit;
		data.resize((data_size + 1)*(data_size + 1));
		data_digit.resize((data_size + 1) * (data_size + 1));
		for (int i = 0; i < data_size; i++){
			data[i].resize(data_size + 1);
			data_digit[i].resize(data_size + 1);
			for (int j = 0; j < data_size; j++){
				cin >> data[i][j];
				int tmp = data[i][j];
				data_digit[i][j] = 0;
				while (tmp){
					tmp /= 10;
					data_digit[i][j]++;
				}
			}
		}
		data[data_size].resize(data_size + 1);
		data_digit[data_size].resize(data_size + 1);
		for (int i = 0; i < data_size; i++){
			int sum = 0;
			for (int j = 0; j < data_size; j++){
				sum += data[i][j];
			}
			data[i][data_size] = sum;
			int tmp = sum;
			data_digit[i][data_size] = 0;
			while (tmp){
				tmp /= 10;
				data_digit[i][data_size]++;
			}

		}
		for (int i = 0; i < data_size + 1; i++){
			int sum = 0;
			for (int j = 0; j < data_size; j++){
				sum += data[j][i];
			}
			data[data_size][i] = sum;
			int tmp = sum;
			data_digit[data_size][i] = 0;
			while (tmp){
				tmp /= 10;
				data_digit[data_size][i]++;
			}
		}

		for (int i = 0; i < data_size + 1; i++){
			for (int j = 0; j < data_size + 1; j++){
				for (int k = data_digit[i][j]; k < 5; k++){
					cout << " ";
				}
				cout << data[i][j];
			}
			cout << "\n";
		}
		cin >> data_size;
	}
	return 0;
}