#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<vector>
using namespace std;
const int maxn=51;
int n;
long long mat[maxn][maxn],map[maxn][maxn];
long long tot[maxn][maxn],tot2[maxn][maxn];
bool check(int x){
for (int i=1;i<=n-x+1;++i)
for (int j=1;j<=n-x+1;++j){
int tops=i+x-1; int topx=j+x-1;
long long C=tot[tops][topx]-tot[i-1][topx]-tot[tops][j-1]+tot[i-1][j-1];
for (int k=1;k<=n-x+1;++k)
for (int l=1;l<=n-x+1;++l){
int u=k+x-1; int v=l+x-1;
long long S=tot2[u][v]-tot2[k-1][v]-tot2[u][l-1]+tot2[k-1][l-1];
if (S!=C) continue;
else if(S==C){
bool flag=1;
int dtx=k-i,dty=l-j;
for (int p=k;p<=u;++p){
if (!flag) break;
for (int q=l;q<=v;++q){
if (map[p][q]!=mat[p-dtx][q-dty]){
flag=0;
break;
}
}
}
if (flag) return 1;
}
}
}
return 0;
}
int main(){
freopen("campaign.in","r",stdin);
freopen("campaign.out","w",stdout);
scanf("%d",&n);
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
scanf("%lld",&mat[i][j]);
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
scanf("%lld",&map[i][j]);
for (int j=1;j<=n;++j)
for (int i=1;i<=n;++i){
tot[i][j]=tot[i-1][j]+tot[i][j-1]-tot[i-1][j-1]+mat[i][j];
tot2[i][j]=tot2[i-1][j]+tot2[i][j-1]-tot2[i-1][j-1]+map[i][j];
}
int L=0,R=n+1,mid;
int ans=0;
while (L<=R){
mid=(L+R)>>1;
if (check(mid)) {
L=mid+1;
ans=max(ans,mid);
}
else R=mid-1;
}
printf("%d",ans);
return 0;
}