// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):x)
#define uint unsigned int
#define dbl long double
using namespace std;
// mylittledoge
int gcd(int x, int y) {
if(x > y) swap(x,y);
while(x > 0) {
int z =x;
x =y%x;
y =z;}
return y;}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
vector< vector<int> > H(N,vector<int>(N));
vector< vector<int> > V =H;
for(int i =0; i < N*N; i++) cin >> H[i/N][i%N];
for(int i =0; i < N*N; i++) cin >> V[i/N][i%N];
queue< pair<int,int> > q;
int dx[] ={1,-1,0,0};
int dy[] ={0,0,1,-1};
int C =0;
vector< vector<int> > isC(N,vector<int>(N,-1));
vector<int> sC;
for(int i =0; i < N; i++) for(int j =0; j < N; j++) if(isC[i][j] == -1) {
q.push(make_pair(i,j));
isC[i][j] =C;
int s =1;
while(!q.empty()) {
int x =q.front().ff, y =q.front().ss;
for(int k =0; k < 4; k++) if(max(dx[k]+x,dy[k]+y) < N && min(dx[k]+x,dy[k]+y) >= 0)
if(isC[dx[k]+x][dy[k]+y] == -1 && V[x][y] == V[dx[k]+x][dy[k]+y] && H[x][y] == H[dx[k]+x][dy[k]+y]) {
isC[dx[k]+x][dy[k]+y] =C;
s++;
q.push(make_pair(dx[k]+x,dy[k]+y));}
q.pop();}
sC.push_back(s);
C++;}
map< pair<int,int>, int> T;
vector< vector< pair<int,int> > > G0;
G0.dibs(100000);
for(int i =0; i < N; i++) for(int j =0; j < N; j++) for(int k =0; k < 4; k++)
if(max(dx[k]+i,dy[k]+j) < N && min(dx[k]+i,dy[k]+j) >= 0)
if(isC[i][j] != isC[i+dx[k]][j+dy[k]]) {
int a =H[i][j]-H[i+dx[k]][j+dy[k]];
int b =-V[i][j]+V[i+dx[k]][j+dy[k]];
if(b == 0) continue;
if(b < 0) a *=-1, b *=-1;
int d =gcd(abs(a),b);
if(a < 0) continue;
int t =(T.find(make_pair(a/d,b/d)) == T.end())?T.size():T[make_pair(a/d,b/d)];
T[make_pair(a/d,b/d)] =t;
G0.resize(max((int)G0.size(),t+1));
G0[t].push_back(make_pair(isC[i][j],isC[i+dx[k]][j+dy[k]]));}
int ans =0;
queue<int> qq;
vector<bool> vis(C,false);
vector< vector<int> > G(C);
vector<int> vv;
for(int t =0; t < G0.size(); t++) {
ALL_THE(G0[t],jt) {
G[jt->ff].push_back(jt->ss);
G[jt->ss].push_back(jt->ff);}
ALL_THE(G0[t],jt) if(!vis[jt->ff]) {
int akt =0;
qq.push(jt->ff);
vis[qq.front()] =true;
while(!qq.empty()) {
akt +=sC[qq.front()];
vv.push_back(qq.front());
ALL_THE(G[qq.front()],kt) if(!vis[*kt]) {
vis[*kt] =true;
qq.push(*kt);}
qq.pop();}
ans =max(ans,akt);}
ALL_THE(G0[t],jt) {
G[jt->ff].clear();
G[jt->ss].clear();}
ALL_THE(vv,jt) vis[*jt] =false;
vv.clear();}
cout << ans << "\n";
return 0;}
// look at my code
// my code is amazing
Ly8gaW9zdHJlYW0gaXMgdG9vIG1haW5zdHJlYW0KI2luY2x1ZGUgPGNzdGRpbz4KLy8gYml0Y2ggcGxlYXNlCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDx2ZWN0b3I+CiNpbmNsdWRlIDxzZXQ+CiNpbmNsdWRlIDxtYXA+CiNpbmNsdWRlIDxxdWV1ZT4KI2luY2x1ZGUgPHN0YWNrPgojaW5jbHVkZSA8bGlzdD4KI2luY2x1ZGUgPGNtYXRoP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xpbnQ+ID4gcTsKICAgIGludCBkeFtdID17MSwtMSwwLDB9OwogICAgaW50IGR5W10gPXswLDAsMSwtMX07CiAgICBpbnQgQyA9MDsKICAgIHZlY3RvcjwgdmVjdG9yPGludD4gPiBpc0MoTix2ZWN0b3I8aW50PihOLC0xKSk7CiAgICB2ZWN0b3I8aW50PiBzQzsKICAgIGZvcihpbnQgaSA9MDsgaSA8IE47IGkrKykgZm9yKGludCBqID0wOyBqIDwgTjsgaisrKSBpZihpc0NbaV1bal0gPT0gLTEpIHsKICAgICAgICBxLnB1c2gobWFrZV9wYWlyKGksaikpOwogICAgICAgIGlzQ1tpXVtqXSA9QzsKICAgICAgICBpbnQgcyA9MTsKICAgICAgICB3aGlsZSghcS5lbXB0eSgpKSB7CiAgICAgICAgICAgIGludCB4ID1xLmZyb250KCkuZmYsIHkgPXEuZnJvbnQoKS5zczsKICAgICAgICAgICAgZm9yKGludCBrID0wOyBrIDwgNDsgaysrKSBpZihtYXgoZHhba10reCxkeVtrXSt5KSA8IE4gJiYgbWluKGR4W2tdK3gsZHlba10reSkgPj0gMCkKICAgICAgICAgICAgICAgIGlmKGlzQ1tkeFtrXSt4XVtkeVtrXSt5XSA9PSAtMSAmJiBWW3hdW3ldID09IFZbZHhba10reF1bZHlba10reV0gJiYgSFt4XVt5XSA9PSBIW2R4W2tdK3hdW2R5W2tdK3ldKSB7CiAgICAgICAgICAgICAgICAgICAgaXNDW2R4W2tdK3hdW2R5W2tdK3ldID1DOwogICAgICAgICAgICAgICAgICAgIHMrKzsKICAgICAgICAgICAgICAgICAgICBxLnB1c2gobWFrZV9wYWlyKGR4W2tdK3gsZHlba10reSkpO30KICAgICAgICAgICAgcS5wb3AoKTt9CiAgICAgICAgc0MucHVzaF9iYWNrKHMpOwogICAgICAgIEMrKzt9CiAgICAKICAgIG1hcDwgcGFpcjxpbnQsaW50PiwgaW50PiBUOwogICAgdmVjdG9yPCB2ZWN0b3I8IHBhaXI8aW50LGludD4gPiA+IEcwOwogICAgRzAuZGlicygxMDAwMDApOwogICAgZm9yKGludCBpID0wOyBpIDwgTjsgaSsrKSBmb3IoaW50IGogPTA7IGogPCBOOyBqKyspIGZvcihpbnQgayA9MDsgayA8IDQ7IGsrKykKICAgICAgICBpZihtYXgoZHhba10raSxkeVtrXStqKSA8IE4gJiYgbWluKGR4W2tdK2ksZHlba10raikgPj0gMCkKICAgICAgICAgICAgaWYoaXNDW2ldW2pdICE9IGlzQ1tpK2R4W2tdXVtqK2R5W2tdXSkgewogICAgICAgICAgICAgICAgaW50IGEgPUhbaV1bal0tSFtpK2R4W2tdXVtqK2R5W2tdXTsKICAgICAgICAgICAgICAgIGludCBiID0tVltpXVtqXStWW2krZHhba11dW2orZHlba11dOwogICAgICAgICAgICAgICAgaWYoYiA9PSAwKSBjb250aW51ZTsKICAgICAgICAgICAgICAgIGlmKGIgPCAwKSBhICo9LTEsIGIgKj0tMTsKICAgICAgICAgICAgICAgIGludCBkID1nY2QoYWJzKGEpLGIpOwogICAgICAgICAgICAgICAgaWYoYSA8IDApIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgaW50IHQgPShULmZpbmQobWFrZV9wYWlyKGEvZCxiL2QpKSA9PSBULmVuZCgpKT9ULnNpemUoKTpUW21ha2VfcGFpcihhL2QsYi9kKV07CiAgICAgICAgICAgICAgICBUW21ha2VfcGFpcihhL2QsYi9kKV0gPXQ7CgkJCQlHMC5yZXNpemUobWF4KChpbnQpRzAuc2l6ZSgpLHQrMSkpOwoJCQkJRzBbdF0ucHVzaF9iYWNrKG1ha2VfcGFpcihpc0NbaV1bal0saXNDW2krZHhba11dW2orZHlba11dKSk7fQogCiAgICBpbnQgYW5zID0wOwogICAgcXVldWU8aW50PiBxcTsKICAgIHZlY3Rvcjxib29sPiB2aXMoQyxmYWxzZSk7CiAgICB2ZWN0b3I8IHZlY3RvcjxpbnQ+ID4gRyhDKTsKCXZlY3RvcjxpbnQ+IHZ2OwoJZm9yKGludCB0ID0wOyB0IDwgRzAuc2l6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