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 <bits/stdc++.h>
using namespace std;
int n;
void solve(){
  int table[200][200] = { 0 };

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

int main(){
  while (scanf("%d", &n), n)solve();
  return 0;
}

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

int main(){
	int n;
	while(cin >> n){
		if(n == 0) break;
		else{
			int data[n+2][n+2] ;

			for (int i = 0; i < n+2; ++i){
				for(int j = 0; j < n+2; j++){					
				data[i][j] = 0;
				}
			}
			
			for (int i = 0; i < n; ++i){
				for(int j = 0; j < n; j++){					
				cin >> data[i][j];
				}
			}
			for (int i = 0; i < n; ++i){
				for(int j = 0; j < n; j++){	
					data[i][n] += data[i][j];
				}
			}
			for (int i = 0; i < n+1; ++i){
				for(int j = 0; j < n; j++){	
					data[n][i] += data[j][i];
				}
			}
			for (int i = 0; i < n+1; ++i){
				for(int j = 0; j < n+1; j++){
					cout << data[i][j]<<" ";
				}
				cout << endl;
			}
				
		}
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int main(){
  int n;
  while(true){
  cin>>n;
  if(!n)break;
  int table[n+1][n+1];
  for(int y=0;y<n;y++){
    int s = 0;
    for(int x=0;x<n;x++){
      cin>>table[x][y];
      s+=table[x][y];
    }
    table[n][y] = s;
  }
  for(int x=0;x<n+1;x++){
    int s = 0;
    for(int y=0;y<n;y++){
      s+=table[x][y];
    }
    table[x][n] = s;
  }
  for(int y=0;y<n+1;y++){
    for(int x=0;x<n;x++){
      printf("%5d ",table[x][y]);
    }
    printf("%5d\n",table[n][y]);
  }
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#define MAX_N 110
using namespace std;
int main(){
	int n,i,j;
	int a[MAX_N][MAX_N];
	while(1){
		cin >> n;
		if(n==0)break;
		for(i=0;i<n;i++){
			for(j=0;j<n;j++){
				cin >> a[i][j];
			}
		}

		int sum1=0,sum[MAX_N];
		for(i=0;i<=n;i++){
			for(j=0;j<=n;j++){
				sum[j]+=a[i][j];
				if(i==n){
					a[i][j]=sum[j];
					sum[j]=0;
				}

				sum1+=a[i][j];
				if(j==n){
					a[i][j]=sum1;
					sum1=0;
				}
			}
		}

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

	}
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
using namespace std;
int main(void)
{
	int i,j,hyo[11][11],n,wa[4],wa2[4],sum;
	while(1) {
		scanf("%d",&n);
		if(n==0)  break;
		for(i=0;i<n;i++) {
			for(j=0;j<n;j++) scanf("%d",&hyo[i][j]);
		}
		for(i=0;i<n;i++) {
			sum=0;
			for(j=0;j<n;j++) sum+=hyo[i][j];
			wa[i]=sum;
		}
		for(i=0;i<n;i++) {
			sum=0;
			for(j=0;j<n;j++) sum+=hyo[j][i];
			wa2[i]=sum;
		}
		for(i=0;i<n;i++) {
			printf(" ");
			for(j=0;j<n;j++) {
				if(hyo[i][j]==100) printf(" %d ",hyo[i][j]);
				if(hyo[i][j]<100 && hyo[i][j]>=10) printf("  %d ",hyo[i][j]);
			}
			printf("%d\n",wa[i]);
		}
		printf(" ");
		for(i=0;i<4;i++) {
			if(wa2[i]>=1000) printf("%d ",wa2[i]);
			if(wa2[i]>=100 && wa2[i]<1000) printf(" %d ",wa2[i]);
			else printf("   %d ",wa2[i]);
		}
		sum=0;
		for(i=0;i<4;i++) {
			sum+=wa[i];
		}
		if(sum==1000) printf("%d\n",sum);
		if(sum<1000 && sum>=10) printf("%d\n",sum);
	}
	return 0;
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#define pb(in,tmp) in.push_back(tmp)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) loop(i,0,b)
#define all(in) in.begin(),in.end()
using namespace std;
int in[11][11]={0};
int main(){
int n;
while(cin>>n,n){
rep(i,n){
int sum=0,j;
for(j=0;j<n;j++){
int tmp;
cin>>tmp;
sum+=tmp;
in[i][j]=tmp;
printf("%5d",tmp);
}
in[i][j]=sum;
printf("%5d\n",sum);
}

rep(i,n+1){
int sum=0;
rep(j,n){
sum+=in[j][i];
}
if(i!=n)printf("%5d",sum);
else printf("%5d\n",sum);
}
}
}

C++: #include <iostream>
using namespace std;
int main(){
	int n;
	int pt[11][11];
	int sum=0;
	while (true){
		cin >> n;
		if (n == 0) break;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				cin >> pt[i][j];
				sum += pt[i][j];
			}
			pt[i][n] = sum;
			sum = 0;
		}
		for (int i = 0; i < n + 1; i++){
			for (int j = 0; j < n; j++){
				sum += pt[j][i];
			}
			pt[n][i] = sum;
			sum = 0;
		}
		for (int i = 0; i < n + 1; i++){
			for (int j = 0; j < n + 1; j++){
				cout << pt[i][j] << ' ';
			}
			cout << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
int main(){
  int t[100][100];
  int n;
  while( cin >> n,n ){
  int c_sum[100],sum[100];
  for(int i = 0 ; i <= n ; i++){
    c_sum[i] = 0;
    sum[i] = 0;
  }
  for(int i = 0 ; i < n ; i++){
    for(int j = 0 ; j < n ; j++){
      cin >> t[j][i];
      sum[i] += t[j][i]; 
      c_sum[j] += t[j][i];
    }
  }
  for(int i = 0 ; i < n ; i++){
    for(int j = 0 ; j < n ; j++){
      printf("%3d ",t[j][i]);
    }
    c_sum[n] += sum[i];
    printf("%3d\n",sum[i]);
  }
  for(int i = 0 ; i < n ; i++)printf("%3d ",c_sum[i]);
  printf("%3d\n",c_sum[n]);
  }
  return 0;
}

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

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <numeric>
#include <sstream>
#include <queue>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int (i)=0; (i)<(n); (i)++)
#define FOR(i,a,b) for(int (i)=(a); (i)<(b); (i)++)
#define PUSH(n,v) for(int i=0; i<(n); i++) {int j; cin >> j; v.push_back(j);}
#define ALL(v) v.begin(), v.end()
#define print(s) cout << (s) << endl

void solve102(int n) {
    vector<int> v(n,0);
    vector<int> w(n,0);
    int k;
    REP(i,n) {
        REP(j,n) {
            cin >> k;
            printf("%5d", k);
            v[i] += k;
            w[j] += k;
        }
        printf("%5d\n", v[i]);
    }
    REP(i,n) {
        printf("%5d", w[i]);
    }
    printf("%5d\n", accumulate(ALL(v), 0));
}

int main() {
    int n;
    while(cin >> n, n) {
        solve102(n);
    }
    return 0;
}