#include <bits/stdc++.h>
#define Nmax 305
#define DIM 70000
using namespace std;
int poz=DIM-1;
char buffer[DIM];
void read(int &x)
{
x=0;
while(!isdigit(buffer[poz]))
if(++poz==DIM)
{
poz=0;
fread(buffer,1,DIM,stdin);
}
while(isdigit(buffer[poz]))
{
x=x*10+buffer[poz]-'0';
if(++poz==DIM)
{
poz=0;
fread(buffer,1,DIM,stdin);
}
}
}
int a[Nmax][Nmax];
//bool viz[Nmax][Nmax];
int M[Nmax][Nmax];
class cmp
{
public:
bool operator () (const pair <int,int> &lsh, const pair <int,int> &rsh) const
{
return a[lsh.first][lsh.second]<a[rsh.first][rsh.second];
}
};
priority_queue < pair <int,int>, vector <pair <int,int> >, cmp> pq;
int x1,y1,x2,y2;
const int dx[]={0,0,-1,1};
const int dy[]={-1,1,0,0};
int minn=INT_MAX,maxx=INT_MIN;
void BFS()
{
while(!pq.empty()) pq.pop();
memset(M,0,sizeof(M));
int i,j,iu,ju,k;
pq.push({x1,y1});
M[x1][y1]=a[x1][y1];
while(!pq.empty())
{
i=pq.top().first;
j=pq.top().second;
pq.pop();
for(k=0;k<4;k++)
{
iu=i+dx[k];
ju=j+dy[k];
if(a[iu][ju] and !M[iu][ju])
{
if(iu==x2 and ju==y2)
{
printf("%d\n",min(M[i][j],a[iu][ju]));
return;
}
else
{
pq.push({iu,ju});
M[iu][ju]=min(M[i][j],a[iu][ju]);
}
}
}
}
}
/*deque <pair <int,int> > q;
bool BFS(int x)
{
if(a[x1][y1]<x) return false;
q.clear();
memset(viz,false,sizeof(viz));
int i,j,iu,ju,k;
q.push_back({x1,y1});
while(!q.empty())
{
i=q.front().first;
j=q.front().second;
q.pop_front();
for(k=0;k<4;k++)
{
iu=i+dx[k];
ju=j+dy[k];
if(a[iu][ju]>=x and !viz[iu][ju])
{
if(iu==x2 and ju==y2) return true;
q.push_back({iu,ju});
viz[iu][ju]=true;
}
}
}
return false;
}
int BS()
{
int p=minn,q=maxx,m,ans;
while(p<=q)
{
m=(p+q)>>1;
if(BFS(m))
{
ans=m;
p=m+1;
}
else q=m-1;
}
return ans;
}*/
int main()
{
freopen("matrice2.in","r",stdin);
freopen("matric2.out","w",stdout);
int n,Q,i,j;
read(n);
read(Q);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
read(a[i][j]);
//minn=min(minn,a[i][j]);
//maxx=max(maxx,a[i][j]);
}
while(Q--)
{
read(x1);
read(y1);
read(x2);
read(y2);
//g<<BS()<<'\n';
BFS();
}
return 0;
}