#include using namespace std; int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }(); using LL = long long; using ULL = unsigned long long; using LD = long double; using PII = pair; using VI = vector; using MII = map; template void cmin(T &x,const T &y) { if(y void cmax(T &x,const T &y) { if(x bool ckmin(T &x,const T &y) { return y bool ckmax(T &x,const T &y) { return x void cmin(T &x,T &y,const T &z) {// x<=y<=z if(z void cmax(T &x,T &y,const T &z) {// x>=y>=z if(x fa; UF(int n_=0):n(n_),fa(n_) { for(int i=0;i> getgrps() { vector id(n, -1); vector > grps; for (int i = 0; i < n; i++) { if (id[find(i)] == -1) { id[find(i)] = grps.size(); grps.emplace_back(); } grps[id[find(i)]].push_back(i); } return grps; } }; template struct YCombinatorResult { Func func; template explicit YCombinatorResult(T &&func) : func(std::forward(func)) {} template decltype(auto) operator()(Args &&...args) { return func(std::ref(*this), std::forward(args)...); } }; template decltype(auto) y_comb(Func &&fun) { return YCombinatorResult>(std::forward(fun)); } /* ---------1---------2---------3---------4---------5---------6---------7--------- 1234567890123456789012345678901234567890123456789012345678901234567890123456789 */ int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; void solve() { int n, m; cin >> n >> m; int nv = n * m; vector> a(n, vector(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } auto getid = [&](int x, int y) -> int { return x * m + y; }; auto getpos = [&](int id) -> PII { return {id / m, id % m}; }; int q; cin >> q; vector> qs(nv); int stx, sty, edx, edy; int u, v; for (int i = 0; i < q; i++) { cin >> stx >> sty >> edx >> edy; stx--; sty--; edx--; edy--; u = getid(stx, sty); v = getid(edx, edy); qs[u].push_back({v, i}); qs[v].push_back({u, i}); } vector rk; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { rk.push_back({i, j}); } } sort(rk.begin(), rk.end(), [&](const PII& x, const PII& y){ return a[x.first][x.second] < a[y.first][y.second]; }); vector> vis(n, vector(m)); vector> g(nv); UF uf(nv); int rt = -1; for (auto&& [x, y] : rk) { vis[x][y] = 1; int id = getid(x, y); // cerr << "id " << id << ' ' << x << ' ' << y << endl; for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 0 || n <= nx || ny < 0 || m <= ny) continue; if (!vis[nx][ny]) continue; int idv = uf.find(getid(nx, ny)); if (uf.merge(idv, id)) { g[id].push_back(idv); // cerr << "add " << id << ' ' << idv << endl; } } rt = id; } uf.init(nv); vector vis2(nv); vector ans(q); y_comb([&](auto dfs, int u) -> void { // cerr << "dfs " << u << endl; vis2[u] = 1; for (int v : g[u]) { dfs(v); uf.merge(v, u); } for (auto&& [v, id] : qs[u]) { if (!vis2[v]) continue; // cerr << "solve " << u << ' ' << v << ' ' << id << ' '; auto [x, y] = getpos(uf.find(v)); // cerr << x << ' ' << y << ' ' << a[x][y] << endl; ans[id] = a[x][y]; } })(rt); for (int i = 0; i < q; i++) { cout << ans[i] << '\n'; } } int main() { int t = 1; // cin >> t; while (t--) { solve(); } return 0; }