#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<sstream>
#include<cmath>
#include<algorithm>
#include<map>
#include<functional>
#include<queue>
#include<stack>
#include<set>
using namespace std;
int stoi(string x){stringstream ss;ss<<x;int tmp;ss>>tmp;return tmp;}
string itos(int x){stringstream ss;ss<<x;return ss.str();}
int main(){
	int ta[100][100],sum[100];
	int n,all;
	while(cin>>n,n){
	all=0;
	for(int i=0;i<100;i++)sum[i]=0,fill(ta[i],ta[i]+100,0);
	for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>ta[i][j];
	for(int i=0;i<n;i++){
		int tnow=0;
		for(int j=0;j<n;j++){
		string ss=itos(ta[i][j]);
		for(int i=0;i<5-ss.size();i++)cout<<" ";	
		cout<<ta[i][j];
		sum[j]+=ta[i][j];
		tnow+=ta[i][j];
		all+=ta[i][j];
		}
		string ss=itos(tnow);
		for(int i=0;i<5-ss.size();i++)cout<<" ";
		cout<<tnow<<endl;
	}
	for(int i=0;i<n;i++){
	string ss=itos(sum[i]);
	if(i==0)for(int i=0;i<5-ss.size();i++)cout<<" ";
	cout<<sum[i];

	for(int i=0;i<5-ss.size();i++)cout<<" ";
	}
	cout<<all<<endl;
	}

	return 0;
}

/*#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<functional>
#include<queue>
#include<stack>
#include<set>
using namespace std;
const int INF=1111111111;
const int M=3000000;
int n,m,h[3000],w[3000],seg[8192],s,tn,ah[3000000],aw[3000000];
void init(int _n){
	s=1;
	while(s<_n)s*=2;
	for(int i=0;i<s*2-1;i++)seg[i]=0;
}
void upgrade(int k,int a){//k???????????????a????????´
	k+=s-1;
	seg[k]=a;
	while(k>0){
	k=(k-1)/2;
	seg[k]=seg[k*2+1]+seg[k*2+2];
	}
}
//a???b???????¨????????±???????
//
int sum(int a,int b,int k,int l,int r){
	cout<<a<<" "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
	if(r<=a||b<=l)return 0;
	if(a<=l&&r<=b){cout<<"use "<<k<<endl;return seg[k];}
	else {
		int vl=sum(a,b,k*2+1,l,(l+r)/2);
		int vr=sum(a,b,k*2+2,(l+r)/2,r);
		return vl+vr;
	}
}
int main(){

	while(cin>>n>>m,n||m){
	tn=n;
	for(int i=0;i<n;i++)cin>>h[i];
	
	init(n);
	
	for(int i=0;i<n;i++){
	upgrade(i,h[i]);
	}
	int a,b;
	for(int i=0;i<s*2-1;i++)cout<<seg[i]<<" ";cout<<endl;
	cout<<sum(1,2,0,0,s)<<endl<<endl;
	for(int i=0;i<n;i++)ah[h[i]]++;
	for(int i=0;i<n;i++){
		for(int j=i+1;j<n;j++){
			//ah[sum(i,j,0,0,n)]++;
			cout<<"n "<<i<<" "<<j<<" "<<sum(i,j,0,0,n)<<endl;
		}
	}
	//for(int i=0;i<1600;i++)if(ah[i]!=0)cout<<i<<" "<<ah[i]<<endl;
	
	for(int i=0;i<m;i++)cin>>w[i];
	tn=m;
	init(m);
	for(int i=0;i<m;i++){upgrade(i,w[i]);}
	for(int i=0;i<m;i++)aw[w[i]]++;
	for(int i=0;i<m;i++){
		for(int j=i+1;j<m;j++){
			aw[sum(i,j,0,0,n)]++;
			cout<<"m "<<sum(i,j,0,0,n)<<endl;
		}
	}
	int ans=0;
	for(int i=1;i<3000000;i++){ans+=ah[i]*aw[i];}
	cout<<ans<<endl;
	fill(ah,ah+3000000,0);
	fill(aw,aw+300000,0);
	}
	return 0;
}*/

     
/*#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<functional>
#include<queue>
#include<stack>
#include<set>
using namespace std;     

int ta[100][100];
const int BAN=10;
int main(){
 char ttmp;
int y,x,s,cnt=0,ma=0;
while(cin>>x>>ttmp>>y>>ttmp>>s){
	x+=2,y+=2;
	if(s==1){
	 ta[y][x]++;
	 ta[y+1][x]++;
	 ta[y][x-1]++;
	 ta[y-1][x]++;
	 ta[y][x+1]++;
	}
	if(s==2||s==3){
	for(int i=-1;i<=1;i++)for(int j=-1;j<=1;j++)ta[y+i][x+j]++;
	}
	if(s==3){
	ta[y+2][x]++;
	ta[y][x+2]++;
	ta[y-2][x]++;
	ta[y][x-2]++;
	}
}//endwhile
/*for(int i=2;i<BAN+2;i++){
	for(int j=2;j<BAN+2;j++){
	cout<<ta[i][j];
	}
	cout<<endl;
}
for(int i=0+2;i<BAN+2;i++){
	for(int j=0+2;j<BAN+2;j++){
	if(ta[i][j]==0)cnt++;
	ma=max(ma,ta[i][j]);
	}
}
cout<<cnt<<endl<<ma<<endl;
return 0;
}*/




/*#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<functional>
#include<queue>
#include<stack>
#include<set>
using namespace std;
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
	double x1,y1,x2,y2,x3,y3,x4,y4;
	bool f=0;
	cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
	double yy1=(y2-y1),yy2=y4-y3;
	if(yy1==0)yy1=0.0000001;
	if(yy2==0)yy2=0.0000001;
	double r1=(x2-x1) / (y2-y1);
	double r2=(x4-x3) / (y4-y3);
	if(abs(r1-r2 ) <=0.000001  )f=1;
	if(f)cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	}
	return 0;
}*/

/*#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<functional>
#include<queue>
#include<stack>
#include<set>
using namespace std;
int stoi(string x){stringstream ss;ss<<x;int tmp;ss>>tmp;return tmp;}
string itos(int x){stringstream ss;ss<<x;return ss.str();}
char ta[50][50];
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
char a[]={'R','D','L','U'};
vector<char>ans;
void dfs(int y,int x,char now){
	bool f=1;
	for(int i=0;i<4;i++){
		if(a[i]==now&&ta[y+dy[i]][x+dx[i]]=='1'){
			ans.push_back(a[i]);
			f=1;
			dfs(y+dy[i],x+dx[i],now);
		}
	}
	if(f){
		for(int i=0;i<4;i++){
			if(ta[y+dy[i]][x+dx[i]]=='1'){
				ans.push_back(a[i]);
				dfs(y+dy[i],x+dx[i],a[i]);
			}
		}
	}
}

int main(){


	for(int i=0;i<9;i++){
		for(int j=0;j<4;j++){
			char now;
			cin>>now;
			ta[i][j]=now;
		}
	}
		dfs(0,0,'F');
		for(int i=0;i<ans.size();i++)cout<<ans[i];
		ans.clear();
	return 0;
}*/


/*#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<functional>
#include<queue>
#include<stack>
#include<set>
using namespace std;
int stoi(string x){stringstream ss;ss<<x;int tmp;ss>>tmp;return tmp;}
string itos(int x){stringstream ss;ss<<x;return ss.str();}
char ta[12][12];

bool ok(int y,int x){
	if(y<0||x<0||y>7||x>7)return 0;
	if(ta[y][x]=='1')return 1;
	return 0;
}
int main(){

while(cin>>ta[0][0]){
	int y,x;
	bool f=1;
	for(int i=0;i<8;i++){
		for(int j=(i==0);j<8;j++){
			char now;
			cin>>now;
			if(now=='1'){
			y=i,x=j;
	if( ok(y,x+1)&ok(y+1,x)&&ok(y+1,x+1) ){cout<<"A";f=0;}
	
	else if( ok(y+1,x)&&ok(y+2,x)&&ok(y+3,x) ){cout<<"B";f=0;}
	
	else if( ok(y,x+1)&&ok(y,x+2)&&ok(y,x+3) ){cout<<"C";f=0;}
	
	else if( ok(y+2,x-1)&&ok(y+1,x-1)&&ok(y+1,x)){cout<<"C";f=0;}
	
	else if( ok(y,x+1)&ok(y+1,x+1)&&ok(y+1,x+2) ){cout<<"E";f=0;}
	
	else if( ok(y+1,x)&&ok(y+1,x+1) &&ok(y+2,x+1)){cout<<"F";f=0;}
	
	else if( ok(y,x+1)&&ok(y+1,x)&&ok(y+1,x-1) ){cout<<"G";f=0;}
	
	if(f==0){cout<<endl;goto end;}
			}
			ta[i][j]=now;
		}
	}
	end:;
		for(int i=0;i<8;i++)for(int j=0;j<8;j++)ta[i][j]='0';
}
	
	return 0;
}*/