#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef LOCAL # include "debug_print.hpp" # define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define debug(...) (static_cast(0)) #endif using namespace std; #define rep1(a) for(int i = 0; i < a; i++) #define rep2(i, a) for(int i = 0; i < a; i++) #define rep3(i, a, b) for(int i = a; i < b; i++) #define rep4(i, a, b, c) for(int i = a; i < b; i += c) #define overload4(a, b, c, d, e, ...) e #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rrep1(a) for(int i = (a)-1; i >= 0; i--) #define rrep2(i, a) for(int i = (a)-1; i >= 0; i--) #define rrep3(i, a, b) for(int i = (b)-1; i >= a; i--) #define rrep4(i, a, b, c) for(int i = (b)-1; i >= a; i -= c) #define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__) #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ); #define pb push_back using ll = long long; using D = double; using LD = long double; using P = pair; using vi = vector; using vl = vector; template using vc = vector; template using vvc = vector>; template using vvvc = vector>; template using vvvvc = vector>; template using vvvvvc = vector>; #define vv(type, name, h, ...) \ vector> name(h, vector(__VA_ARGS__)) #define vvv(type, name, h, w, ...) \ vector>> name( \ h, vector>(w, vector(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector>>> name( \ a, vector>>( \ b, vector>(c, vector(__VA_ARGS__)))) template using PQ = priority_queue>; template using minPQ = priority_queue, greater>; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b ostream &operator<<(ostream &os, const pair &p) { os << p.first << " " << p.second; return os; } template istream &operator>>(istream &is, pair &p) { is >> p.first >> p.second; return is; } template ostream &operator<<(ostream &os, const vector &v) { int s = (int)v.size(); for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i]; return os; } template istream &operator>>(istream &is, vector &v) { for (auto &x : v) is >> x; return is; } void in() {} template void in(T &t, U &...u) { cin >> t; in(u...); } void out() { cout << "\n"; } template void out(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << sep; out(u...); } void outr() {} template void outr(const T &t, const U &...u) { cout << t; outr(u...); } #include "atcoder/dsu.hpp" const int dx[4]={0,1,0,-1}; const int dy[4]={1,0,-1,0}; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int h,w; in(h,w); vv(int,a,h,w); in(a); rep(i,h)rep(j,w) a[i][j]--; vc> a_idx(h*w); rep(i,h)rep(j,w){ a_idx[a[i][j]].emplace_back(i,j); } int q; in(q); vi rs(q),cs(q),rt(q),ct(q); rep(i,q) in(rs[i],cs[i],rt[i],ct[i]); rep(i,q) rs[i]--, cs[i]--, rt[i]--, ct[i]--; vi ok(q,h*w), ng(q,-1); while(true){ vc mid_idx(h*w); bool fin = true; rep(i,q)if(ok[i]-ng[i]>1){ fin = false; mid_idx[(ok[i]+ng[i])/2].pb(i); } if(fin) break; atcoder::dsu d(h*w); rep(k,h*w){ for(auto [ni,nj] : a_idx[k]){ rep(l,4){ int pi = ni+dx[l]; int pj = nj+dy[l]; if(pi < 0 or pi >= h or pj < 0 or pj >= w) continue; if(a[pi][pj] <= k) d.merge(pi*w+pj, ni*w+nj); } } for(auto qi : mid_idx[k]){ if(d.same(rs[qi]*w+cs[qi], rt[qi]*w+ct[qi])){ ok[qi] = k; } else ng[qi] = k; } } } rep(i,q) out(ok[i]+1); }