import java.io.*; import java.util.*; class Node { int x, y, pre, num; Node(int x, int y, int pre, int num) { this.x = x; this.y = y; this.pre = pre; this.num = num; } } public class Main1175 { static int dir[][] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; static boolean hash[][] = new boolean[1005][1005]; static int map[][] = new int[1005][1005]; static int x1, y1, x2, y2; static int n, m; static PrintWriter cout = new PrintWriter (new OutputStreamWriter(System.out)); static boolean index(Node now) { if(now.x >= 0 && now.x < n && now.y >= 0 && now.y < m && now.num <= 2 && map[now.x][now.y] == 0) return true; return false; } static void BFS() { if(map[x1][y1] != map[x2][y2] || map[x1][y1] == 0 || map[x2][y2] == 0) { cout.println("NO"); return ; } for(int i = 0; i <= n; i++) for(int j = 0; j <= m; j++) hash[i][j] = false; int tx, ty, tpre, tnum; int mx, my; Queue<Node> Q = new LinkedList<Node>(); for(int i = 0; i < 4; i++) { tx = x1 + dir[i][0]; ty = y1 + dir[i][1]; tpre = i; tnum = 0; Node next = new Node(tx, ty, tpre, tnum); hash[x1][y1] = true; if(tx == x2 && ty == y2 && tnum <= 2 ) { cout.println("YES"); cout.flush(); return ; } if(!index(next)) continue; if(!hash[next.x][next.y] ) { Q.add(next); hash[next.x][next.y] = true; } mx = next.x; my = next.y; while(true) { tx = mx + dir[i][0]; ty = my + dir[i][1]; tpre = i; tnum = 0; if(tx == x2 && ty == y2 && tnum <= 2 ) { cout.println("YES"); cout.flush(); return ; } Node mid = new Node(tx, ty, tpre, tnum); if(index(mid)) { if(!hash[mid.x][mid.y]) { Q.add(mid); hash[mid.x][mid.y] = true; } } else break; mx = mid.x; my = mid.y; } } Node now = new Node(0, 0, 0, 0); while(!Q.isEmpty()) { now = Q.poll(); if(now.x == x2 && now.y == y2 ) { cout.println("YES"); return ; } if (now.num >= 2) { continue; } mx = now.x; my = now.y; tpre = now.pre; tnum = now.num; while(true) { tx = mx + dir[tpre][0]; ty = my + dir[tpre][1]; if(tx == x2 && ty == y2 && tnum <= 2) { cout.println("YES"); return ; } Node mid = new Node(tx, ty, tpre, tnum); if(index(mid)) { if(!hash[mid.x][mid.y]) { Q.add(mid); hash[mid.x][mid.y] = true; } } else break; mx = mid.x; my = mid.y; } for(int i = 0; i < 4; i++) { if(i == now.pre) continue; tx = now.x + dir[i][0]; ty = now.y + dir[i][1]; tpre = i; tnum = now.num + 1; if(tx == x2 && ty == y2 && tnum <= 2 ) { cout.println("YES"); return ; } Node next = new Node(tx, ty, tpre, tnum); if(!index(next)) continue; if(!hash[next.x][next.y]) { Q.add(next); hash[next.x][next.y] = true; } mx = next.x; my = next.y; tpre = next.pre; tnum = next.num; while(true) { tx = mx + dir[i][0]; ty = my + dir[i][1]; Node mid = new Node(tx, ty, tpre, tnum); if(tx == x2 && ty == y2 && tnum <= 2 ) { cout.println("YES"); return ; } if(index(mid)) { if(!hash[mid.x][mid.y]) { Q.add(mid); hash[mid.x][mid.y] = true; } } else break; mx = mid.x; my = mid.y; } } } cout.println("NO"); return ; } public static void main(String[] args) throws IOException { // Scanner cin = new Scanner(System.in); StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); //while(cin.hasNext()) while(in.nextToken() != StreamTokenizer.TT_EOF) { //n = cin.nextInt(); n = (int)in.nval; //m = cin.nextInt(); in.nextToken(); m = (int)in.nval; if(n == 0 && m == 0) break; for(int i = 0 ;i < n; i++) for(int j = 0; j < m; j++) { //map[i][j] = cin.nextInt(); in.nextToken(); map[i][j] = (int)in.nval; } int t; // t = cin.nextInt(); in.nextToken(); t = (int)in.nval; while(t-- > 0) { // x1 = cin.nextInt(); in.nextToken(); x1 = (int)in.nval; x1--; // y1 = cin.nextInt(); in.nextToken(); y1 = (int)in.nval; y1--; // x2 = cin.nextInt(); in.nextToken(); x2 = (int)in.nval; x2--; // y2 = cin.nextInt(); in.nextToken(); y2 = (int)in.nval; y2--; BFS(); } } cout.flush(); } }