#include <bits/stdc++.h>
using namespace std;
#define int long long
#define NM 102
int N,x[NM*NM],g[NM*NM][4],s[NM*NM],v[NM*NM],w[NM*NM],p[NM*NM],t[NM*NM],r[NM*NM],ti[NM*NM],to[NM*NM],ta;
void dfs(int i){
s[i]=1;
w[i]=i;
ti[i]=ta++;
for(int j:g[i]){
if(x[j]!=x[i]){
continue;
}
if(j==p[i]){
continue;
}
if(v[j]){
if(v[j]<v[w[i]]){
w[i]=j;
}
continue;
}
v[j]=v[i]+1;
p[j]=i;
t[j]=t[i];
dfs(j);
s[i]+=s[j];
if(v[w[j]]<v[w[i]]){
w[i]=w[j];
}
}
to[i]=ta++;
}
int des(int i,int j){
return ti[i]<=ti[j]&&to[i]>=to[j];
}
int bf(int i,int j){
swap(x[i],x[j]);
vector<int>v(NM*NM);
auto dfs=[&](auto self,int i)->int{
if(v[i]){
return 0;
}
v[i]=1;
for(int j:g[i]){
if(x[j]!=x[i]){
continue;
}
v[i]+=self(self,j);
}
return v[i];
};
int ans=dfs(dfs,i)*dfs(dfs,j);
swap(x[i],x[j]);
return ans;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N=10;
cin>>N;
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
x[i*NM+j]=rand()%4+1;
cin>>x[i*NM+j];
}
}
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
g[i*NM+j][0]=i*NM+j-1;
g[i*NM+j][1]=i*NM+j+1;
g[i*NM+j][2]=(i-1)*NM+j;
g[i*NM+j][3]=(i+1)*NM+j;
}
}
int o=0;
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
int y=i*NM+j;
if(v[y]){
continue;
}
o++;
v[y]=1;
t[y]=o;
r[o]=y;
dfs(y);
}
}
for(int d=0;d<2;d++){
for(int i=1;i+(d==1)<=N;i++){
for(int j=1;j+(d==0)<=N;j++){
int y1=i*NM+j;
int y2=(i+(d==1))*NM+j+(d==0);
if(t[y1]==t[y2]){
cout<<0<<' ';
continue;
}
set<int>dbg;
int c[2]{1,1};
for(int i=0;i<2;i++){
int b=0;
for(int z:g[y2]){
if(z==y1){
continue;
}
if(x[z]!=x[y1]){
continue;
}
if(t[z]==t[y1]){
if(des(y1,z)){
b|=1;
}
else{
b|=2;
}
continue;
}
c[i]+=s[r[t[z]]];
for(int zz:g[y2]){
if(zz<z&&z!=y1&&t[zz]==t[z]){
c[i]-=s[r[t[z]]];
break;
}
}
}
if(b){
dbg.insert(b);
if(b==1){
for(int k:g[y1]){
if(x[k]!=x[y1]){
continue;
}
if(v[k]!=v[y1]+1){
continue;
}
for(int kk:g[y2]){
if(kk==y1){
continue;
}
if(t[kk]!=t[y1]){
continue;
}
if(des(k,kk)){
if(v[w[k]]<v[y1]){
b=2;
dbg.insert(22);
}
c[i]+=s[k];
break;
}
}
}
if(b==2){
c[i]+=s[r[t[y1]]]-s[y1];
b=1;
}
}
if(b==2){
for(int k:g[y1]){
if(x[k]!=x[y1]){
continue;
}
if(v[k]!=v[y1]+1){
continue;
}
if(v[w[k]]<v[y1]){
c[i]+=s[k];
}
}
c[i]+=s[r[t[y1]]]-s[y1];
}
if(b==3){
c[i]+=s[r[t[y1]]]-1;
for(int k:g[y1]){
if(x[k]!=x[y1]){
continue;
}
if(v[k]!=v[y1]+1){
continue;
}
if(w[k]!=k){
continue;
}
c[i]-=s[k];
for(int kk:g[y2]){
if(kk==y1){
continue;
}
if(t[kk]!=t[y1]){
continue;
}
if(des(k,kk)){
c[i]+=s[k];
break;
}
}
}
}
}
swap(y1,y2);
}
cout<<c[0]*c[1]<<' ';
if(c[0]*c[1]!=bf(y1,y2)){
cout.flush();
system("cls");
cout<<endl;
cout<<endl;
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
cout<<x[i*NM+j]<<' ';
}
cout<<'\n';
}
cout<<endl;
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
cout<<v[i*NM+j]<<','<<v[w[i*NM+j]]<<' ';
}
cout<<'\n';
}
cout<<endl;
for(int i:dbg){
cout<<i<<' ';
}
cout<<endl;
cout<<endl;
cout<<(y1/NM)<<' '<<(y1%NM)<<endl;
cout<<(y2/NM)<<' '<<(y2%NM)<<endl;
cout<<"got "<<c[0]<<'x'<<c[1]<<endl;
cout<<"exp "<<bf(y1,y2)<<'\n';
exit(1);
}
}
cout<<'\n';
}
}
if(0){
cout<<"OK"<<endl;
next:
memset(x,0,sizeof(x));
memset(g,0,sizeof(g));
memset(s,0,sizeof(s));
memset(v,0,sizeof(v));
memset(w,0,sizeof(w));
memset(p,0,sizeof(p));
memset(t,0,sizeof(t));
memset(r,0,sizeof(r));
memset(ti,0,sizeof(ti));
memset(to,0,sizeof(to));
ta=0;
main();
}
return 0;
}