#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 1005;
int maze[MAXN][MAXN]; //迷宫数组
bool vis[MAXN][MAXN]; //访问数组
bool flag; //是否能消除
int n, m, q; //输入
int sx, sy, ex, ey; //起点,终点
int dicx[4] = { 1,-1,0,0 }; //x轴移动方向
int dicy[4] = { 0,0,1,-1 }; //y轴移动方向
//深搜,x,y为当前坐标,dic为当前方向,turns为当前转弯次数
void dfs(int x, int y, int dic, int turns)
{
//转弯次数大于2或者已经找到,停止
if (turns > 2 || flag)
return;
//转弯次数为2,且目前与目标不在同一直线上,剪枝
if (turns == 2 && (x - ex) != 0 && (y - ey) != 0)
return;
if (x == ex && y == ey && turns <= 2) //到达目标,搜索成功
{
flag = 1;
return;
}
for (int i = 0; i < 4; ++i) //搜索四个方向
{
int nx = x + dicx[i]; //下一步的x坐标
int ny = y + dicy[i]; //下一步的y坐标
if (nx < 1 || nx > n || ny < 1 || ny > m || vis[nx][ny]) //边界和访问情况
continue;
if (maze[nx][ny] == 0 || (nx == ex && ny == ey))
{
vis[nx][ny] = 1;
if (dic == -1 || dic == i) //目前是第一步或者前进方向没有变化
dfs(nx, ny, i, turns);
else //前进方向变化
dfs(nx, ny, i, turns + 1);
vis[nx][ny] = 0; //回溯
}
}
return;
}
int main()
{
while (cin >> n >> m)
{
if (n == 0 && m == 0)
break;
memset(maze, 0, sizeof(maze));
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
cin >> maze[i][j];
}
cin >> q;
for (int i = 0; i < q; ++i)
{
cin >> sx >> sy >> ex >> ey;
memset(vis, 0, sizeof(vis));
flag = 0;
if (maze[sx][sy] == maze[ex][ey] && maze[sx][sy] != 0)
dfs(sx, sy, -1, 0); //将初始方向设为-1
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
return 0;
}