#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
ifstream fin("matrice2.in");
ofstream fout("matrice2.out");
int n,i,j,nr,q,vmx,k,sz,ct,p;
int v[305][305],z[305][305],rz[20005];
int dl[]={-1,0,1,0},cl[]={0,1,0,-1};
struct per
{ int x,y;
bool operator==(const per &other)const
{ return (x==other.x && y==other.y); }
} t[305][305], h;
struct poz
{ int v,x,y;
bool operator<(const poz &other)const
{ return v<other.v; }
} s[90005];
struct points
{ int v,l,r,x1,y1,x2,y2,id;
bool operator<(const points &alt)const
{ return v<alt.v; }
} pt[20005],nw[20005];
per tata(int x,int y)
{
per p={x,y};
if(p==t[x][y]) return p;
t[x][y]=tata(t[x][y].x, t[x][y].y);
return t[x][y];
}
void prepare(int val)
{
int x,y;
while(s[p].v>=val && p>=0)
{
z[s[p].x][s[p].y]=1;
for(int j=0;j<4;j++)
{
x=s[p].x+dl[j];
y=s[p].y+cl[j];
if(z[x][y] && x>0 && x<=n && y>0 && y<=n)
{
tata(x, y);
if(t[x][y].x!=s[p].x || t[x][y].y!=s[p].y)
t[t[x][y].x][t[x][y].y]={s[p].x, s[p].y};
}
}
p--;
}
}
int main() {
fin>>n>>q;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fin>>v[i][j], s[++k]={v[i][j],i,j}, vmx=max(vmx, v[i][j]);
sort(s+1,s+k+1);
for(i=1;i<=q;i++)
{
fin>>pt[i].x1>>pt[i].y1>>pt[i].x2>>pt[i].y2;
pt[i].id=i;
pt[i].v=vmx/2;
pt[i].l=1; pt[i].r=vmx;
}
sz=q;
for(;sz;)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
t[i][j]={i,j}, z[i][j]=0;
ct=0;
p=k;
for(i=sz;i>=1;i--)
{
prepare(pt[i].v);
if(tata(pt[i].x1, pt[i].y1)==tata(pt[i].x2, pt[i].y2))
pt[i].l=pt[i].v;
else pt[i].r=pt[i].v-1;
if(pt[i].l<pt[i].r)
{
pt[i].v=(pt[i].l+pt[i].r+1)/2;
nw[++ct]=pt[i];
}
else rz[pt[i].id]=pt[i].l;
}
sort(nw+1, nw+ct+1);
sz=ct;
for(i=1;i<=ct;i++)
pt[i]=nw[i];
}
for(i=1;i<=q;i++)
fout<<rz[i]<<"\n";
}