#include <bits/stdc++.h>
#define endl '\n'
#define pii pair <int, int>
#define fi first
#define se second
//#define int long long
#define el cout << endl;
#define spc(n) fixed << setprecision(n)
#define ed cout << "\n__________________________________\n"
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 505;
const int oo = 1e9;
int n, q, lim;
int a[N][N], dist[N][N];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
bool check(int s, int t, int e1, int e2, int mid)
{
queue <pii> q;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dist[i][j] = oo;
q.push({s, t});
dist[s][t] = 0;
while(q.size())
{
int u = q.front().fi;
int v = q.front().se;
q.pop();
for (int k = 0; k < 4; k++)
{
int x = u + dx[k];
int y = v + dy[k];
if (x < 1 || x > n || y < 1 || y > n || a[x][y] < mid) continue;
if (dist[x][y] > dist[u][v] + 1)
{
dist[x][y] = dist[u][v] + 1;
q.push({x, y});
}
}
}
return dist[e1][e2] != oo;
}
void sub12()
{
while(q--)
{
int u, v, x, y;
cin >> u >> v >> x >> y;
int l = 1, r = lim, ans = lim;
while(l <= r)
{
int mid = (l + r) >> 1;
if (check(u, v, x, y, mid))
{
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
cout << ans << endl;
}
}
struct graph
{
int u, v, w;
}; vector <graph> edge;
struct cmpw
{
bool operator()(const graph &x, const graph &y)
const
{
return x.w > y.w;
}
};
const int LOG = 18;
const int M = 260000;
int cnt;
int par[M], high[M], p[M][20], mn[M][20], node[N][N];
vector <pii> g[M];
void makeset()
{
for (int i = 1; i <= n * n; i++) par[i] = i;
}
int findset(int u)
{
if (par[u] == u) return u;
return (par[u] = findset(par[u]));
}
void DSU(int u, int v, int w)
{
int x = findset(u), y = findset(v);
if (x == y) return;
par[y] = x;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
void DFS(int u, int par)
{
for (pii x : g[u])
{
int v = x.fi, w = x.se;
if (v == par) continue;
high[v] = high[u] + 1;
p[v][0] = u;
mn[v][0] = w;
DFS(v, u);
}
}
void init()
{
for (int j = 1; j <= LOG; j++)
for (int i = 1; i <= n * n; i++)
{
p[i][j] = p[p[i][j - 1]][j - 1];
mn[i][j] = min(mn[i][j - 1], mn[p[i][j - 1]][j - 1]);
}
}
void setup()
{
int cnt = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
node[i][j] = ++cnt;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
int u = node[i][j];
for (int k = 0; k < 4; k++)
{int x = i + dx[k];
int y = j + dy[k];
if (x < 1 || x > n || y < 1 || y > n) continue;
int v = node[x][y];
int w = min(a[i][j], a[x][y]);
edge.push_back({u, v, w});
}
}
makeset();
sort(edge.begin(), edge.end(), cmpw());
for (graph x : edge)
{
int u = x.u, v = x.v, w = x.w;
DSU(u, v, w);
}
DFS(1, -1);
init();
}
int LCA(int u, int v)
{
int ans = oo;
if (high[u] < high[v]) swap(u, v);
int h = high[u] - high[v];
for (int i = LOG; i >= 0; i--)
if ((h >> i) & 1)
{
ans = min(ans, mn[u][i]);
u = p[u][i];
}
if (u == v) return ans;
for (int i = LOG; i >= 0; i--)
if (p[u][i] != p[v][i])
{
ans = min(ans, mn[u][i]);
ans = min(ans, mn[v][i]);
u = p[u][i];
v = p[v][i];
}
return ans = min({ans, mn[u][0], mn[v][0]});
}
void sub34()
{
setup();
while(q--)
{
int u, v, x, y;
cin >> u >> v >> x >> y;
cout << LCA(node[u][v], node[x][y]) << endl;
}
}
signed main ()
{
// freopen("file.inp", "r", stdin);
// freopen("file.out", "w", stdout);
IOS;
cin >> n >> q;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
cin >> a[i][j];
lim = max(lim, a[i][j]);
}
// if (n <= 100 && q <= 2000) sub12();
// else
sub34();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZW5kbCAnXG4nCiNkZWZpbmUgcGlpIHBhaXIgPGludCwgaW50PgojZGVmaW5lIGZpIGZpcnN0CiNkZWZpbmUgc2Ugc2Vjb25kCi8vI2RlZmluZSBpbnQgbG9uZyBsb25nCiNkZWZpbmUgZWwgY291dCA8PCBlbmRsOwojZGVmaW5lIHNwYyhuKSBmaXhlZCA8PCBzZXRwcmVjaXNpb24obikKI2RlZmluZSBlZCBjb3V0IDw8ICJcbl9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19cbiIKI2RlZmluZSBJT1MgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsgIGNpbi50aWUoMCk7IGNvdXQudGllKDApOwp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKCmNvbnN0IGludCBOID0gNTA1Owpjb25zdCBpbnQgb28gPSAxZTk7CgoKaW50IG4sIHEsIGxpbTsKaW50IGFbTl1bTl0sIGRpc3RbTl1bTl07CgppbnQgZHhbXSA9IHswLCAwLCAtMSwgMX07CmludCBkeVtdID0gezEsIC0xLCAwLCAwfTsKCmJvb2wgY2hlY2soaW50IHMsIGludCB0LCBpbnQgZTEsIGludCBlMiwgaW50IG1pZCkKewogICAgcXVldWUgPHBpaT4gcTsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkrKykKICAgICAgICBmb3IgKGludCBqID0gMTsgaiA8PSBuOyBqKyspCiAgICAgICAgICAgIGRpc3RbaV1bal0gPSBvbzsKCiAgICBxLnB1c2goe3MsIHR9KTsKICAgIGRpc3Rbc11bdF0gPSAwOwoKICAgIHdoaWxlKHEuc2l6ZSgpKQogICAgewogICAgICAgIGludCB1ID0gcS5mcm9udCgpLmZpOwogICAgICAgIGludCB2ID0gcS5mcm9udCgpLnNlOwogICAgICAgIHEucG9wKCk7CgogICAgICAgIGZvciAoaW50IGsgPSAwOyBrIDwgNDsgaysrKQogICAgICAgIHsKICAgICAgICAgICAgaW50IHggPSB1ICsgZHhba107CiAgICAgICAgICAgIGludCB5ID0gdiArIGR5W2tdOwogICAgICAgICAgICBpZiAoeCA8IDEgfHwgeCA+IG4gfHwgeSA8IDEgfHwgeSA+IG4gfHwgYVt4XVt5XSA8IG1pZCkgY29udGludWU7CiAgICAgICAgICAgIGlmIChkaXN0W3hdW3ldID4gZGlzdFt1XVt2XSArIDEpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGRpc3RbeF1beV0gPSBkaXN0W3VdW3ZdICsgMTsKICAgICAgICAgICAgICAgIHEucHVzaCh7eCwgeX0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiBkaXN0W2UxXVtlMl0gIT0gb287Cn0KCgp2b2lkIHN1YjEyKCkKewogICAgd2hpbGUocS0tKQogICAgewogICAgICAgIGludCB1LCB2LCB4LCB5OwogICAgICAgIGNpbiA+PiB1ID4+IHYgPj4geCA+PiB5OwoKICAgICAgICBpbnQgbCA9IDEsIHIgPSBsaW0sIGFucyA9IGxpbTsKICAgICAgICB3aGlsZShsIDw9IHIpCiAgICAgICAgewogICAgICAgICAgICBpbnQgbWlkID0gKGwgKyByKSA+PiAxOwogICAgICAgICAgICBpZiAoY2hlY2sodSwgdiwgeCwgeSwgbWlkKSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgYW5zID0gbWlkOwogICAgICAgICAgICAgICAgbCA9IG1pZCArIDE7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSByID0gbWlkIC0gMTsKICAgICAgICB9CiAgICAgICAgY291dCA8PCBhbnMgPDwgZW5kbDsKICAgIH0KfQoKCnN0cnVjdCBncmFwaAp7CiAgICBpbnQgdSwgdiwgdzsKfTsgIHZlY3RvciA8Z3JhcGg+IGVkZ2U7CgpzdHJ1Y3QgY21wdwp7CiAgICBib29sIG9wZXJhdG9yKCkoY29uc3QgZ3JhcGggJngsIGNvbnN0IGdyYXBoICZ5KQogICAgY29uc3QKICAgIHsKICAgICAgICByZXR1cm4geC53ID4geS53OwogICAgfQp9OwoKY29uc3QgaW50IExPRyA9IDE4Owpjb25zdCBpbnQgTSA9IDI2MDAwMDsKaW50IGNudDsKaW50IHBhcltNXSwgaGlnaFtNXSwgcFtNXVsyMF0sIG1uW01dWzIwXSwgbm9kZVtOXVtOXTsKdmVjdG9yIDxwaWk+IGdbTV07Cgp2b2lkIG1ha2VzZXQoKQp7CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuICogbjsgaSsrKSBwYXJbaV0gPSBpOwp9CgppbnQgZmluZHNldChpbnQgdSkKewogICAgaWYgKHBhclt1XSA9PSB1KSByZXR1cm4gdTsKICAgIHJldHVybiAocGFyW3VdID0gZmluZHNldChwYXJbdV0pKTsKfQoKdm9pZCBEU1UoaW50IHUsIGludCB2LCBpbnQgdykKewogICAgaW50IHggPSBmaW5kc2V0KHUpLCB5ID0gZmluZHNldCh2KTsKICAgIGlmICh4ID09IHkpIHJldHVybjsKICAgIHBhclt5XSA9IHg7CiAgICBnW3VdLnB1c2hfYmFjayh7diwgd30pOwogICAgZ1t2XS5wdXNoX2JhY2soe3UsIHd9KTsKfQoKdm9pZCBERlMoaW50IHUsIGludCBwYXIpCnsKICAgIGZvciAocGlpIHggOiBnW3VdKQogICAgewogICAgICAgIGludCB2ID0geC5maSwgdyA9IHguc2U7CiAgICAgICAgaWYgKHYgPT0gcGFyKSBjb250aW51ZTsKICAgICAgICBoaWdoW3ZdID0gaGlnaFt1XSArIDE7CiAgICAgICAgcFt2XVswXSA9IHU7CiAgICAgICAgbW5bdl1bMF0gPSB3OwogICAgICAgIERGUyh2LCB1KTsKICAgIH0KfQoKdm9pZCBpbml0KCkKewogICAgZm9yIChpbnQgaiA9IDE7IGogPD0gTE9HOyBqKyspCiAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbiAqIG47IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIHBbaV1bal0gPSBwW3BbaV1baiAtIDFdXVtqIC0gMV07CiAgICAgICAgICAgIG1uW2ldW2pdID0gbWluKG1uW2ldW2ogLSAxXSwgbW5bcFtpXVtqIC0gMV1dW2ogLSAxXSk7CiAgICAgICAgfQp9Cgp2b2lkIHNldHVwKCkKewogICAgaW50IGNudCA9IDA7CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspCiAgICAgICAgZm9yIChpbnQgaiA9IDE7IGogPD0gbjsgaisrKQogICAgICAgICAgICBub2RlW2ldW2pdID0gKytjbnQ7CgogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKQogICAgICAgIGZvciAoaW50IGogPSAxOyBqIDw9IG47IGorKykKICAgICAgICB7CiAgICAgICAgICAgIGludCB1ID0gbm9kZVtpXVtqXTsKICAgICAgICAgICAgZm9yIChpbnQgayA9IDA7IGsgPCA0OyBrKyspCiAgICAgICAgICAgIHtpbnQgeCA9IGkgKyBkeFtrXTsKICAgICAgICAgICAgICAgIGludCB5ID0gaiArIGR5W2tdOwogICAgICAgICAgICAgICAgaWYgKHggPCAxIHx8IHggPiBuIHx8IHkgPCAxIHx8IHkgPiBuKSBjb250aW51ZTsKICAgICAgICAgICAgICAgIGludCB2ID0gbm9kZVt4XVt5XTsKICAgICAgICAgICAgICAgIGludCB3ID0gbWluKGFbaV1bal0sIGFbeF1beV0pOwogICAgICAgICAgICAgICAgZWRnZS5wdXNoX2JhY2soe3UsIHYsIHd9KTsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICBtYWtlc2V0KCk7CiAgICBzb3J0KGVkZ2UuYmVnaW4oKSwgZWRnZS5lbmQoKSwgY21wdygpKTsKICAgIGZvciAoZ3JhcGggeCA6IGVkZ2UpCiAgICB7CiAgICAgICAgaW50IHUgPSB4LnUsIHYgPSB4LnYsIHcgPSB4Lnc7CiAgICAgICAgRFNVKHUsIHYsIHcpOwogICAgfQoKICAgIERGUygxLCAtMSk7CiAgICBpbml0KCk7Cn0KCmludCBMQ0EoaW50IHUsIGludCB2KQp7CiAgICBpbnQgYW5zID0gb287CiAgICBpZiAoaGlnaFt1XSA8IGhpZ2hbdl0pIHN3YXAodSwgdik7CiAgICBpbnQgaCA9IGhpZ2hbdV0gLSBoaWdoW3ZdOwogICAgZm9yIChpbnQgaSA9IExPRzsgaSA+PSAwOyBpLS0pCiAgICAgICAgaWYgKChoID4+IGkpICYgMSkKICAgICAgICB7CiAgICAgICAgICAgIGFucyA9IG1pbihhbnMsIG1uW3VdW2ldKTsKICAgICAgICAgICAgdSA9IHBbdV1baV07CiAgICAgICAgfQogICAgaWYgKHUgPT0gdikgcmV0dXJuIGFuczsKCiAgICBmb3IgKGludCBpID0gTE9HOyBpID49IDA7IGktLSkKICAgICAgICBpZiAocFt1XVtpXSAhPSBwW3ZdW2ldKQogICAgICAgIHsKICAgICAgICAgICAgYW5zID0gbWluKGFucywgbW5bdV1baV0pOwogICAgICAgICAgICBhbnMgPSBtaW4oYW5zLCBtblt2XVtpXSk7CiAgICAgICAgICAgIHUgPSBwW3VdW2ldOwogICAgICAgICAgICB2ID0gcFt2XVtpXTsKICAgICAgICB9CiAgICByZXR1cm4gYW5zID0gbWluKHthbnMsIG1uW3VdWzBdLCBtblt2XVswXX0pOwp9Cgp2b2lkIHN1YjM0KCkKewogICAgc2V0dXAoKTsKICAgIHdoaWxlKHEtLSkKICAgIHsKICAgICAgICBpbnQgdSwgdiwgeCwgeTsKICAgICAgICBjaW4gPj4gdSA+PiB2ID4+IHggPj4geTsKICAgICAgICBjb3V0IDw8IExDQShub2RlW3VdW3ZdLCBub2RlW3hdW3ldKSA8PCBlbmRsOwogICAgfQp9CgpzaWduZWQgbWFpbiAoKQp7CiAgICAvLyBmcmVvcGVuKCJmaWxlLmlucCIsICJyIiwgc3RkaW4pOwogICAgLy8gZnJlb3BlbigiZmlsZS5vdXQiLCAidyIsIHN0ZG91dCk7CiAgICBJT1M7CiAgICBjaW4gPj4gbiA+PiBxOwogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKQogICAgICAgIGZvciAoaW50IGogPSAxOyBqIDw9IG47IGorKykKICAgICAgICB7CiAgICAgICAgICAgIGNpbiA+PiBhW2ldW2pdOwogICAgICAgICAgICBsaW0gPSBtYXgobGltLCBhW2ldW2pdKTsKICAgICAgICB9CgovLyAgICBpZiAobiA8PSAxMDAgJiYgcSA8PSAyMDAwKSBzdWIxMigpOwovLyAgICBlbHNlCiAgICBzdWIzNCgpOwoKICAgIHJldHVybiAwOwp9Cg==