import sys, time, random from collections import deque, Counter, defaultdict input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 61 - 1 mod = 998244353 class dsu(): n=1 parent_or_size=[-1 for i in range(n)] def __init__(self,N): self.n=N self.num = N self.parent_or_size=[-1 for i in range(N)] def merge(self,a,b): assert 0<=a0: result2.append(result[i]) return result2 def count(self): return self.num h, w = mi() a = [li() for _ in range(h)] q = ii() S = [[v - 1 for v in li()] for _ in range(q)] SXSY = [] for i in range(q): sx, sy, tx, ty = S[i] SXSY.append((sx* w + sy, tx * w + ty)) cost = [] for i in range(h): for j in range(w): cost.append((i * w + j, a[i][j])) cost.sort(key = lambda x: x[1]) ok = [h * w] * q ng = [0] * q dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] dz = 'UDLR' MID = [[] for _ in range(h * w)] mid = [0] * q EDGE = [[] for _ in range(h * w)] for i in range(h * w): X, c = cost[i] x = X // w y = X % w for j in range(4): nx = x + dx[j] ny = y + dy[j] if 0 <= nx < h and 0 <= ny < w and a[nx][ny] <= c: EDGE[i].append(nx * w + ny) ans = [] for _ in range(18): MID = [[] for _ in range(h * w)] for i in range(q): mid[i] = (ok[i] + ng[i]) // 2 MID[mid[i]].append(i) U = dsu(h * w) for i in range(h * w): u, c = cost[i] for v in EDGE[i]: U.merge(u, v) for j in MID[i]: s, t = SXSY[j] if U.same(s, t): ok[j] = mid[j] else: ng[j] = mid[j] for i in range(q): print(cost[ok[i]][1])