#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<deque>
#include<queue>
#include<set>
#include<vector>
using namespace std;
const int INF = 1000000005;
const int NMAX = 305;
const int HMAX = 1000005;
const int QMAX = 20005;
int N, Q;
int a[NMAX][NMAX];
int x1, f1, x2, f2;
int rez[NMAX][NMAX];
int lin[] = {-1, 0, 0, 1};
int col[] = {0, -1, 1, 0};
pair<int, int> poz[NMAX*NMAX];
pair<int, int> tata[NMAX][NMAX];
int exista[HMAX], hm, NR = 1;
int c[QMAX][5], sol[QMAX];
int rg[NMAX][NMAX];
inline bool nul(pair<int, int> x)
{
if(!x.first && !x.second)
return 1;
return 0;
}
void citi()
{
scanf("%d %d", &N, &Q);
for(int i = 1 ; i <= N ; i++)
for(int j = 1 ; j <= N ; j++)
{
scanf("%d", &a[i][j]);
poz[(i - 1)*N + j] = make_pair(i, j);
exista[a[i][j]]++;
hm = max(hm, a[i][j]);
}
for(int i = 1 ; i <= Q ; i++)
scanf("%d%d%d%d", &c[i][1], &c[i][2], &c[i][3], &c[i][4]);
}
inline bool in(int x, int y)
{
if(1 <= x && x <= N && 1 <= y && y <= N)
return 1;
return 0;
}
inline pair<int, int> find(int x, int y)
{
pair<int, int> rad = tata[x][y];
for(; rad != tata[rad.first][rad.second]; rad = tata[rad.first][rad.second]);
while(x != tata[x][y].first || y != tata[x][y].second)
{
int x1 = x, y1 = y;
x = tata[x1][y1].first;
y = tata[x1][y1].second;
tata[x1][y1] = rad;
}
return rad;
}
inline void unite(pair<int, int> x, pair<int, int> y)
{
if(rg[x.first][x.second] > rg[y.first][y.second])
tata[y.first][y.second] = x;
else
tata[x.first][x.second] = y;
if(rg[x.first][x.second] == rg[y.first][y.second])
rg[y.first][y.second]++;
}
void scrie()
{
for(int i = 1 ; i <= Q ; i++)
printf("%d\n", sol[i]);
}
inline bool cmp(pair<int, int> x, pair<int, int> y)
{
return a[x.first][x.second] >= a[y.first][y.second];
}
int main()
{
freopen("matrice2.in", "r", stdin);
freopen("matrice2.out", "w", stdout);
citi();
a[0][0] = INF;//ma ajuta la soratre , car face fite
sort(poz, poz + N * N + 1, cmp);
for(int i = hm ; i ; i--)
if(exista[i])
{
for( ; a[poz[NR].first][poz[NR].second] == i ; NR++)
{
int x = poz[NR].first, y = poz[NR].second;
rez[x][y] = 1;
tata[x][y] = make_pair(x, y);
rg[x][y] = 1;
for(int k = 0 ; k <= 3 ; k++)
if(in(x + lin[k], y + col[k]) && rez[x + lin[k]][y + col[k]])
unite(find(x, y), find(x + lin[k], y + col[k]));
}
for(int q = 1 ; q <= Q ; q++)
if(!sol[q] && !nul(tata[c[q][1]][c[q][2]]) && !nul(tata[c[q][3]][c[q][4]]))
{
pair<int, int> x = find(c[q][1], c[q][2]), y = find(c[q][3], c[q][4]);
if(x == y)
sol[q] = i;
}
}
scrie();
return 0;
}