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 "bits/stdc++.h"

#define REP(i,num) for(int i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto& x:c){cout<<x<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=int>
T in(){T x; cin >> x; return (x);}
template<typename T=int,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

int main(){
	//cin.tie(0);
	//ios::sync_with_stdio(false);
	
	while(true){
		int N=in();
		if(!N) break;
		vector<vector<int>> T(N+1,vector<int>(N+1,0));
		REP(i,N){
			REP(j,N){
				T[i][j] = in();
				T[N][j] += T[i][j];
				T[i][N] += T[i][j];
			}
		}
		T[N][N] = accumulate(T[N].begin(),T[N].begin()+N,0);
		REP(i,N+1){
			REP(j,N+1){
				printf("%5d",T[i][j]);
			}
			printf("\n");
		}
	}
	
	return 0;
}

C++: #include<stdio.h>
#include<memory.h>
int main(){
	int n;
	int i,j,a[110][110]={0};
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		for(i=1;i<=n;i++){
			for(j=1;j<=n;j++){
				scanf("%d",&a[i][j]);
				a[i][n+1]+=a[i][j];
				a[n+1][j]+=a[i][j];
			}
		}
		for(j=1;j<=n;j++){
			a[n+1][n+1]+=a[n+1][j];
		}
		for(i=1;i<=n+1;i++){
			for(j=1;j<=n+1;j++){
				printf("%5d",a[i][j]);
			}
			printf("\n");
		}
		memset(a,0,sizeof(a));
	}
	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++){
	  if(x!=0){cout<<" ";}
	  ketasu=getKetasu(t[x][w]);
	  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<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define rp(a) while(a--)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
int main(){
	int n;
	while(cin>>n,n){
		int in[15][15]={0};
		rep(i,n)rep(j,n)cin>>in[i][j];
		rep(i,n){
			int sum=0;
			rep(j,n)sum+=in[i][j];
			in[i][n]=sum;
		}
		rep(i,n+1){
			int sum=0;
			rep(j,n)sum+=in[j][i];
			in[n][i]=sum;
		}
		rep(i,n+1){
			rep(j,n+1)printf("%5d",in[i][j]);
			cout<<endl;
		}
	}
}

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

int main(){
	int n,i,j;
	while(cin >> n && n){
		int table[11][11] = {{0}};
		for(i = 0;i < n;i++){
			for(j = 0;j < n;j++){
				cin >> table[i][j];
				table[i][n] += table[i][j];
				table[n][j] += table[i][j];
			}
			table[n][n] += table[i][n];
		}
		for(i = 0;i <= n;i++){
			for(j = 0;j <= n;j++){
				if(table[i][j] / 100 != 0) cout << "  ";
				else cout << "   ";
				cout << table[i][j];
			}
			if(i - 1 != n)cout << "\n";
		}
	}
	return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int n,x[101][101],i,j,sum,y;
	while(1){
		scanf("%d",&n);
		if(n==0) break;
		for(i=0;i<n;i++){
			for(j=0;j<n;j++) scanf("%d",&x[i][j]);
		}
		for(i=0;i<n;i++){
			sum=0;
			for(j=0;j<n;j++) sum+=x[j][i];
			x[n][i]=0;
			x[n][i]=sum;
		}
		for(i=0;i<=n;i++){
			sum=0;
			for(j=0;j<n;j++) sum+=x[i][j];
			x[i][n]=0;
			x[i][n]=sum;
		}
		y=0;
		for(i=0;i<=n;i++){
			for(j=0;j<=n;j++){
				y=x[i][j];
				while(y<1000){
					printf(" ");
					y*=10;
				}
				printf("%d ",x[i][j]);
			}
			printf("\n");
		}
	}
	return 0;
}

C++: 
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<complex>
#include<cstdio>
using namespace std ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef vector<string> vs ;
typedef pair<int, int> pii; 
typedef long long ll ;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()


int main(void){
	int n;	
	while(cin>>n,n){	
		vvi ma(n+1,vi(n+1));
		rep(i,n){
			rep(j,n){
				cin>>ma[i][j];
				ma[n][j]+=ma[i][j];
				ma[i][n]+=ma[i][j];
			}
		}
		rep(i,n){
			ma[n][n]+=ma[i][n];
		}
		rep(i,n+1){
			rep(j,n+1){
				printf("%5d",ma[i][j]);
			}
			cout<<endl;
		}
	}
}

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("%5d",t[j][i]);
    }
    c_sum[n] += sum[i];
    printf("%5d\n",sum[i]);
  }
  for(int i = 0 ; i < n ; i++)printf("%5d",c_sum[i]);
  printf("%5d\n",c_sum[n]);
  }
  return 0;
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

signed main() {
	int n;
	while (cin >> n, n) {
		vector<vector<int>>v(n + 1, vector<int>(n + 1));
		rep(i, 0, n)rep(j, 0, n)cin >> v[i][j];
		rep(i, 0, n) {
			v[i][n] = accumulate(v[i].begin(), v[i].end(), 0);
		}
		rep(i, 0, n + 1) {
			int sum(0);
			rep(j, 0, n)sum += v[j][i];
			v[n][i] = sum;
		}

		rep(i, 0, n + 1) {
			rep(j, 0, n + 1) {
				printf("%5d", v[i][j]);
			}
			cout << endl;
		}
	}
	return 0;
}