#include<stdio.h> int move[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; int n,m,map[1005][1005],x2,y2,right,visit[1005][1005]; void dfs(int x,int y,int dir,int k) { int i; if(right) return; if(k>2)//转弯次数超限 return; if(x<=0||y<=0||x>n||y>m)//边界 return; if(x==x2&&y==y2)//找到 { right=1; return; } if(k==2&&x-x2!=0&&y-y2!=0)//剪枝:转弯次数到达上限时,现处坐标与目标坐标未在同一条线上 return; if(map[x][y]!=0) return; if(visit[x][y]) return; visit[x][y]=1; for(i=0;i<4;i++) { if(i!=dir) dfs(x+move[i][0],y+move[i][1],i,k+1); else dfs(x+move[i][0],y+move[i][1],dir,k); } visit[x][y]=0; } int main() { int i,j,q,x1,y1; while(~scanf("%d%d",&n,&m)&&n+m) { for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { scanf("%d",&map[i][j]); } } scanf("%d",&q); while(q--) { scanf("%d%d%d%d",&x1,&y1,&x2,&y2); if(map[x1][y1]==0||map[x2][y2]==0||map[x1][y1]!=map[x2][y2]) printf("NO\n"); else { right=0; memset(visit,0,sizeof(visit)); for(i=0;i<4;i++) { dfs(x1+move[i][0],y1+move[i][1],i,0); } if(right) printf("YES\n"); else printf("NO\n"); } } } return 0; }