#include using namespace std; using ll = long long; #define rep(i, a, b) for(int i = a; i < b; i++) const int INF = 1e9; int h, w, sx, sy, q; vector s; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; int dy[] = {1, -1, 0, 0, 1, -1, -1, 1}; int p_dsu[2][8][100005]; int dist_val[100005][2]; int find_set(int p, int k, int i) { if (p_dsu[p][k][i] == i) return i; return p_dsu[p][k][i] = find_set(p, k, p_dsu[p][k][i]); } inline int get_nxt(int u, int k) { int x = u / w, y = u % w; int nx = x + dx[k], ny = y + dy[k]; if (nx >= 0 && nx < h && ny >= 0 && ny < w && s[nx][ny] == '.') { return nx * w + ny; } return h * w; } void solve() { if (!(cin >> h >> w >> sx >> sy)) return; sx--; sy--; s.resize(h); rep(i, 0, h) cin >> s[i]; int n = h * w; rep(p, 0, 2) rep(k, 0, 8) rep(i, 0, n + 1) p_dsu[p][k][i] = i; rep(i, 0, n) dist_val[i][0] = dist_val[i][1] = INF; int st = sx * w + sy; dist_val[st][0] = 0; queue> qu; qu.push({st, 0}); rep(k, 0, 8) { p_dsu[0][k][st] = find_set(0, k, get_nxt(st, k)); } while (!qu.empty()) { auto [u, p] = qu.front(); qu.pop(); int np = 1 - p; int d = dist_val[u][p]; rep(k, 0, 8) { int curr = get_nxt(u, k); curr = find_set(np, k, curr); while (curr != n) { dist_val[curr][np] = d + 1; qu.push({curr, np}); rep(dir, 0, 8) { p_dsu[np][dir][curr] = find_set(np, dir, get_nxt(curr, dir)); } curr = find_set(np, k, get_nxt(curr, k)); } } } bool has_deg = false; rep(k, 0, 8) { if (get_nxt(st, k) != n) has_deg = true; } cin >> q; while (q--) { int gx, gy; ll t; cin >> gx >> gy >> t; gx--; gy--; int v = gx * w + gy; int p = t % 2; int min_d = dist_val[v][p]; if (min_d == INF || t < min_d) { cout << "No\n"; } else if (min_d == 0) { if (has_deg) cout << "Yes\n"; else cout << "No\n"; } else { cout << "Yes\n"; } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); return 0; }