#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
int n, m, map[1001][1001], x3, y3, tt[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
bool visit[1001][1001],b;
using namespace std;
int main()
{
void dfs(int, int, int, int);
while (cin >> n >> m && (m + n))
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
scanf_s("%d", &map[i][j]);
int Q; cin >> Q;
while (Q--)
{
memset(visit, false, sizeof(visit));
int x2, y2; b = false;
cin >> x2 >> y2 >> x3 >> y3;
if (map[x2][y2] != map[x3][y3])
{
cout << "NO" << endl; continue;
}
if(map[x2][y2] == 0 || map[x3][y3] == 0)
{
cout << "NO" << endl; continue;
}
if (x2 == x3&&y2 == y3)
{
cout << "NO" << endl; continue;
}
visit[x2][y2] = true;
dfs(x2, y2, -1, 0);
if (b == true) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
return 0;
}
void dfs(int x, int y, int dec, int turn)
{
if (turn > 2) return;
if (b == true) return;
if (turn <= 2 && x == x3&&y == y3)
{
b = true; return;
}
if (turn == 2)
{
if (x != x3&&y != y3) return;
else if (x == x3)
{
if (dec == 0) return;
}
else if (y == y3)
{
if (dec == 1) return;
}
}//剪枝
for (int i = 0; i < 4; i++)
{
if (x + tt[i][0] <= 0 || x + tt[i][0] > n || y + tt[i][1] <= 0 || y + tt[i][1] > m) continue;
if (visit[x + tt[i][0]][y + tt[i][1]] == false)
{
int x4 = x + tt[i][0], y4 = y + tt[i][1];
if ((x4 != x3 || y4 != y3) && map[x4][y4] != 0) continue;
visit[x + tt[i][0]][y + tt[i][1]] = true;
if (dec == -1) dfs(x + tt[i][0], y + tt[i][1], i % 2, turn);
if (i % 2 == dec) dfs(x + tt[i][0], y + tt[i][1], i % 2, turn);
if (i % 2 != dec) dfs(x + tt[i][0], y + tt[i][1], i % 2, turn + 1);
visit[x + tt[i][0]][y + tt[i][1]] = false;
}
}
}
这题目的关键在于剪枝,还有方向的表示 i%2==0 上下 i%2==1 左右