#include #include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define all(x) begin(x), end(x) template bool chmin(T& x, T y) { return x > y ? (x = y, true) : false; } template bool chmax(T& x, T y) { return x < y ? (x = y, true) : false; } void solve() { int h, w, sx, sy; cin >> h >> w >> sx >> sy; sx--, sy--; vector s(h); rep(i, 0, h) cin >> s[i]; vector dx = {0, 1, 1, 1, 0, -1, -1, -1}; vector dy = {1, 1, 0, -1, -1, -1, 0, 1}; auto check = [&](int x, int y) { return 0 <= x && x < h && 0 <= y && y < w && s[x][y] == '.'; }; vector mn(2, vector(8, vector(h, vector(w, 1e9)))); deque> dq; rep(d, 0, 8) { int nx = sx + dx[d], ny = sy + dy[d]; if (check(nx, ny)) { mn[1][d][nx][ny] = 1; dq.push_back({nx, ny, (int)d, 1}); } } while (!dq.empty()) { auto [x, y, d, f] = dq.front(); dq.pop_front(); if (f > mn[f & 1][d][x][y]) continue; int nd = mn[f & 1][d][x][y]; { int nx = x + dx[d], ny = y + dy[d]; if (check(nx, ny) && chmin(mn[nd & 1][d][nx][ny], nd)) { dq.push_front({nx, ny, d, nd}); } } nd++; for (d = 0; d < 8; d++) { int nx = x + dx[d], ny = y + dy[d]; if (check(nx, ny) && chmin(mn[nd & 1][d][nx][ny], nd)) { dq.push_back({nx, ny, d, nd}); } } } vector cs(2, vector(h, vector(w, 1e9))); rep(f, 0, 2) rep(d, 0, 8) rep(i, 0, h) rep(j, 0, w) { chmin(cs[f][i][j], mn[f][d][i][j]); } int q; cin >> q; while (q--) { int qx, qy, t; cin >> qx >> qy >> t; qx--, qy--; if (cs[t & 1][qx][qy] > t) cout << "No\n"; else cout << "Yes\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); int t = 1; // cin >> t; while (t--) solve(); }