/*
#include<stdio.h>
int map[19][19]= {0},rst=0,t[5][2]= {0},cnt=0,tmp0=0,tmp1=0,v[5],e=0;
int a[3]= {0},b[3]= {0},c[3]= {0},d[3]= {0};
void rec(int i, int j)
{
if(v[e]!=v[e]+1)
{
e++;
e%=5;
}
if(map[i][j]==0)
{
return;
}
cnt++;
t[cnt][0]=i;
t[cnt][1]=j;
if(map[i][j]<=20)
{
map[i][j]*=10;
}
switch(v[e])
{
case 0:
if(map[i+1][j+1]==map[i][j]||map[i-1][j-1]==map[i][j]||map[i+1][j+1]*10==map[i][j]||map[i-1][j-1]*10==map[i][j])
{
a[map[i][j]]++;
rec(i+1,j+1);
rec(i-1,j-1 );
}
v[e]=1;
if(map[i+1][j]==map[i][j]||map[i-1][j]==map[i][j]||map[i+1][j]*10==map[i][j]||map[i-1][j]*10==map[i][j])
{
b[map[i][j]]++;
rec(i+1,j);
rec(i-1,j);
}
v[e]=2;
if(map[i][j+1]==map[i][j]||map[i][j-1]==map[i][j]||map[i][j+1]*10==map[i][j]||map[i][j-1]*10==map[i][j])
{
c[map[i][j]]++;
rec(i,j+1);
rec(i,j-1);
}
v[e]=3;
if(map[i+1][j-1]==map[i][j]||map[i-1][j+1]==map[i][j]||map[i+1][j-1]*10==map[i][j]||map[i-1][j+1]*10==map[i][j])
{
d[map[i][j]]++;
rec(i+1,j-1);
rec(i-1,j+1 );
}
v[e]=4;
break;
case 1:
if(map[i+1][j+1]==map[i][j]||map[i-1][j-1]==map[i][j]||map[i+1][j+1]*10==map[i][j]||map[i-1][j-1]*10==map[i][j])
a[map[i][j]]++;
break;
case 2:
if(map[i+1][j]==map[i][j]||map[i-1][j]==map[i][j]||map[i+1][j]*10==map[i][j]||map[i-1][j]*10==map[i][j])
b[map[i][j]]++;
break;
case 3:
if(map[i][j+1]==map[i][j]||map[i][j-1]==map[i][j]||map[i][j+1]*10==map[i][j]||map[i][j-1]*10==map[i][j])
c[map[i][j]]++;
break;
case 4:
if(map[i+1][j-1]==map[i][j]||map[i-1][j+1]==map[i][j]||map[i+1][j-1]*10==map[i][j]||map[i-1][j+1]*10==map[i][j])
d[map[i][j]]++;
break;
}
if((5==a[1]&&a[1]<6)||(5==b[1]&&b[1]<6)||(5==c[1]&&c[1]<6)||(5==d[1]&&d[1]<6))
{
rst=1;
return;
}
if((5==a[2]&&a[2]<6)||(5==b[2]&&b[2]<6)||(5==c[2]&&c[2]<6)||(5==d[2]&&d[2]<6))
{
rst=2;
return;
}
}
int main()
{
int i,j,k,l,u,p;
for(i=0; i<19; i++)
{
for(j=0; j<19; j++)
{
scanf("%d",&map[i][j]);
}
}
for(i=0; i<19; i++)
{
for(j=0; j<19; j++)
{
if(map[i][j]==1||map[i][j]==2)
{
for(k=1; k<=2; k++)
{
a[k]=0;
b[k]=0;
c[k]=0;
d[k]=0;
}
for(k=0; k<5; k++)
{
t[k][0]=0;
t[k][1]=0;
v[k]=0;
}
e=0;
cnt=0;
if(rst!=0)
{
for(k=0; k<5; k++)
{
for(p=0; p<5-1; p++)
{
if(t[p][0]+t[p][1]>t[p+1][0]+t[p+1][1])
{
tmp0=t[p][0];
tmp1=t[p][1];
t[p][0]=t[p+1][0];
t[p][1]=t[p+1][1];
t[p+1][0]=tmp0;
t[p+1][1]=tmp1;
}
}
}
printf("%d\n%d %d",rst,t[0][0],t[0][1]);
return 0;
}
}
}
}
printf("0");
return 0;
}
*/
//max,least
#include<stdio.h>
int n,i,j,k,tmp,cnt=0,a[200][200]={0};
int map[200][200]={0},map2[200][200]={0};
void view()
{
for(int l=1;l<=n;l++){
for(int u=1;u<=n;u++){
printf("%d ",map[l][u]);
}
printf("\n");
}
printf("---------------------------------\n");
}
void rec(int i, int j, int k)
{
if(map[j][k]>=10){
return;
}
map[j][k]*=10;
a[cnt][i]++;
b[cnt][i]
if(map[j+1][k+1]>i){
rec(i,j+1,k+1);
}
if(map[j+1][k]>i){
rec(i,j+1,k);
}
if(map[j][k+1]>i){
rec(i,j,k+1);
}
if(map[j-1][k-1]>i){
rec(i,j-1,k-1);
}
if(map[j-1][k+1]>i){
rec(i,j-1,k+1);
}
if(map[j+1][k-1]>i){
rec(i,j+1,k-1);
}
if(map[j-1][k]>i){
rec(i,j-1,k);
}
if(map[j][k-1]>i){
rec(i,j,k-1);
}
return;
}
int main()
{
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
scanf("%d",&map[i][j]);
map2[i][j]=map[i][j];
}
}
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
for(k=1;k<=n;k++){
map[i][j]=map2[i][j];
}
}
cnt=0;
for(j=1;j<=n;j++){
for(k=1;k<=n;k++){
if(map[j][k]>i&&map[j][k]<10){
cnt++;
rec(i,j,k);
}
}
}
}
for(i=1;i<=n;i++){
for(j=1;j<n;j++){
if(a[j][0]<a[j+1]){
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
}