#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> Tree;
#define sz(x) (int)(x).size()
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define vi vector<int>
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define vpi vector<pair<int, int>>
#define f0r(i,a) for(int i=0;i<a;i++)
#define f1r(i,a,b) for(int i=a;i<b;i++)
#define read1(a) int a; scanf("%d", &a)
#define read2(a,b) int a,b; scanf("%d %d", &a, &b)
#define read3(a,b,c) int a,b,c; scanf("%d %d %d", &a, &b, &c)
#define read(n,arr) int arr[n]; f0r(i,n){ scanf("%d", &arr[i]); }
#define print1(a) printf("%d \n", a)
#define print2(a, b) printf("%d %d \n", a, b)
#define print3(a, b, c) printf("%d %d %d \n", a, b, c)
#define print(v) for (int i : v) { printf("%d ", i); } printf("\n")
#define debug printf("asdf\n");
#define newl printf("\n");
#define usaco(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout);
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
void io(string taskname){
string fin = taskname + ".in";
string fout = taskname + ".out";
const char* FIN = fin.c_str();
const char* FOUT = fout.c_str();
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);
fast_io();
}
const int MAX = 1505;
const int BIG = 2250005;
static int n, m, q;
int vis[BIG][4];
int grid[MAX][MAX];
int reach[BIG];
//checks if location to visit is valid and inbounds and not a hay pile
int valid(int x, int y){
int a = x/m;
int b = x%m;
if(y == 0){ //up
a-= 1;
}
else if(y == 2){ //down
a+= 1;
}
else if(y == 1){ // left
b-= 1;
}
else{ //right
b+= 1;
}
if(a>=0 && a<n && b>=0 && b<m && grid[a][b] == 0){ //not filled with hay
return a*m + b; //return
}
return -1;
}
//finds biconnected components
struct BCC {
int V, ti = 0;
vector<vector<int>> adj;
vector<int> par, disc, low;
vector<vector<pair<int, int>>> fin;
vector<pair<int, int>> st;
vector<int> comp;
void init(int _V) {
V = _V;
par.resize(V), disc.resize(V), low.resize(V), adj.resize(V); comp.resize(V);
for(int i= 0 ; i<V; i++){
par[i] = disc[i] = low[i] = -1;
}
}
void addEdge(int u, int v) {
adj[u].push_back(v), adj[v].push_back(u);
}
void BCCutil(int u) {
disc[u] = low[u] = ti++;
int child = 0;
for (int toAdd = 0; toAdd<4; toAdd++){
int i = valid(u, toAdd);
if(i == -1){
continue;
}
if (i != par[u]) {
if (disc[i] == -1) {
child ++; par[i] = u;
st.push_back({u,i});
BCCutil(i);
low[u] = min(low[u],low[i]);
if ((disc[u] == 0 && child > 1) || (disc[u] != 0 && disc[u] <= low[i])) { // checks for articulation point
vector<pair<int, int>> tmp;
while (st.back() != make_pair(u,i)) tmp.push_back(st.back()), st.pop_back();
tmp.push_back(st.back()), st.pop_back();
fin.push_back(tmp);
}
}
else if (disc[i] < disc[u]) {
low[u] = min(low[u],disc[i]);
st.push_back({u,i});
}
}
}
}
void bcc() {
for(int i = 0; i<V; i++){
if (disc[i] == -1 && grid[i/m][i%m] == 0) {
BCCutil(i);
if (st.size()) fin.push_back(st);
st.clear();
}
}
for(int i = 0; i<fin.size(); i++){
for(int j = 0; j<fin[i].size(); j++){
comp[fin[i][j].f] = i;
comp[fin[i][j].s] = i;
}
}
}
};
BCC b;
void bfs(int cow, int box, int dir){
if(vis[box][dir] == 1){
return;
}
//bfs
list<pii> q;
q.pb(mp(box, cow));
vis[box][dir] = 1;
while(!q.empty()){
cow = q.front().s;
box = q.front().f;
q.pop_front();
for(int i = 0; i<4; i++){
// 0 down, 1 right, 2 up, 3 left
int nxtCow = valid(box, (i+2)%4); //where cow has to be to move box in direction i
int nxtBox = valid(box, i); //the next location of the box moved in direction i
if(nxtCow == -1 || nxtBox == -1){ //checking if they are valid
continue;
}
if((b.comp[cow] != b.comp[nxtCow])){ //seeing if it's possible for the cow to move to that new location
continue;
}
if(vis[nxtBox][i] == 1){
continue;
}
//adding to queue
vis[nxtBox][i] = 1;
q.push_back(mp(nxtBox, box));
}
}
}
void bfs1(int cow, int box){//bfs to find where cow can be around the initial position of box
list<int> q;
q.pb(cow);
reach[cow] =1;
while(!q.empty()){
int s = q.front();
q.pop_front();
f0r(i, 4){
int nxt = valid(s, i);
if(nxt != -1 && nxt != box && reach[nxt] == 0){
reach[nxt] = 1;
q.pb(nxt);
}
}
}
}
int main(){
// io("pushabox");
cin >>n >>m >> q;
string temporary;
getline(cin, temporary);
int cow = 0;
int box = 0;
//read input
f0r(i, n){
string line;
getline(cin, line);
f0r(j, m){
if(line[j] == '#'){
grid[i][j] = 1;
}
if(line[j] == 'A'){
cow = i*m +j;
}
if(line[j] == 'B'){
box = i*m+j;
}
}
}
//find the biconnected components
b.init(n*m);
b.bcc();
//finds where the cow can be around the box
bfs1(cow, box);
f0r(i, 4){
int nxtCow = valid(box, i);
if(nxtCow != -1 && reach[nxtCow] == 1){
//bfs from each location to figure which places you can visit
bfs(nxtCow, box, (i+2)%4);
}
}
f0r(i, q){
int r, c;
cin >> r >> c;
r--; c--;
int loc = r*m + c;
if(vis[loc][0] == 1 || vis[loc][1] == 1 || vis[loc][2] == 1 || vis[loc][3] == 1 || loc == box){
cout << "YES" << endl;
}
else{
cout << "NO" << endl;
}
}
return 0;
}
I3ByYWdtYSBjb21tZW50KGxpbmtlciwgIi9zdGFjazoyMDAwMDAwMDAiKQovLyNwcmFnbWEgR0NDIG9wdGltaXplKCJPZmFzdCIpCi8vI3ByYWdtYSBHQ0MgdGFyZ2V0KCJzc2Usc3NlMixzc2UzLHNzc2UzLHNzZTQscG9wY250LGFibS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AgICBwYXIucmVzaXplKFYpLCBkaXNjLnJlc2l6ZShWKSwgbG93LnJlc2l6ZShWKSwgYWRqLnJlc2l6ZShWKTsgY29tcC5yZXNpemUoVik7CiAgICAgICBmb3IoaW50IGk9IDAgOyBpPFY7IGkrKyl7CiAgICAgICAgICAgIHBhcltpXSA9IGRpc2NbaV0gPSBsb3dbaV0gPSAtMTsKICAgICAgIH0KICAgIH0KICAgIHZvaWQgYWRkRWRnZShpbnQgdSwgaW50IHYpIHsKICAgICAgIGFkalt1XS5wdXNoX2JhY2sodiksIGFkalt2XS5wdXNoX2JhY2sodSk7CiAgICB9CgogICAgdm9pZCBCQ0N1dGlsKGludCB1KSB7CiAgICAgICAgZGlzY1t1XSA9IGxvd1t1XSA9IHRpKys7CiAgICAgICAgaW50IGNoaWxkID0gMDsKICAgICAgICBmb3IgKGludCB0b0FkZCA9IDA7IHRvQWRkPDQ7IHRvQWRkKyspewogICAgICAgICAgICBpbnQgaSA9IHZhbGlkKHUsIHRvQWRkKTsKICAgICAgICAgICAgaWYoaSA9PSAtMSl7CiAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoaSAhPSBwYXJbdV0pIHsKICAgICAgICAgICAgICAgIGlmIChkaXNjW2ldID09IC0xKSB7CiAgICAgICAgICAgICAgICAgICAgIGNoaWxkICsrOyBwYXJbaV0gPSB1OwogICAgICAgICAgICAgICAgICAgICBzdC5wdXNoX2JhY2soe3UsaX0pOwogICAgICAgICAgICAgICAgICAgICBCQ0N1dGlsKGkpOwogICAgICAgICAgICAgICAgICAgICBsb3dbdV0gPSBtaW4obG93W3VdLGxvd1tpXSk7CgogICAgICAgICAgICAgICAgICAgICBpZiAoKGRpc2NbdV0gPT0gMCAmJiBjaGlsZCA+IDEpIHx8IChkaXNjW3VdICE9IDAgJiYgZGlzY1t1XSA8PSBsb3dbaV0pKSB7IC8vIGNoZWNrcyBmb3IgYXJ0aWN1bGF0aW9uIHBvaW50CiAgICAgICAgICAgICAgICAgICAgICAgICB2ZWN0b3I8cGFpcjxpbnQsIGludD4+IHRtcDsKICAgICAgICAgICAgICAgICAgICAgICAgIHdoaWxlIChzdC5iYWNrKCkgIT0gbWFrZV9wYWlyKHUsaSkpIHRtcC5wdXNoX2JhY2soc3QuYmFjaygpKSwgc3QucG9wX2JhY2soKTsKICAgICAgICAgICAgICAgICAgICAgICAgIHRtcC5wdXNoX2JhY2soc3QuYmFjaygpKSwgc3QucG9wX2JhY2soKTsKICAgICAgICAgICAgICAgICAgICAgICAgIGZpbi5wdXNoX2JhY2sodG1wKTsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UgaWYgKGRpc2NbaV0gPCBkaXNjW3VdKSB7CiAgICAgICAgICAgICAgICAgICAgbG93W3VdID0gbWluKGxvd1t1XSxkaXNjW2ldKTsKICAgICAgICAgICAgICAgICAgICBzdC5wdXNoX2JhY2soe3UsaX0pOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIHZvaWQgYmNjKCkgewogICAgICAgICBmb3IoaW50IGkgPSAwOyBpPFY7IGkrKyl7CiAgICAgICAgICAgIGlmIChkaXNjW2ldID09IC0xICYmIGdyaWRbaS9tXVtpJW1dID09IDApIHsKICAgICAgICAgICAgICAgICBCQ0N1dGlsKGkpOwogICAgICAgICAgICAgICAgIGlmIChzdC5zaXplKCkpIGZpbi5wdXNoX2JhY2soc3QpOwogICAgICAgICAgICAgICAgIHN0LmNsZWFyKCk7CiAgICAgICAgICAgICB9CiAgICAgICAgIH0KICAgICAgICAgZm9yKGludCBpID0gMDsgaTxmaW4uc2l6ZSgpOyBpKyspewogICAgICAgICAgICBmb3IoaW50IGogPSAwOyBqPGZpbltpXS5zaXplKCk7IGorKyl7CiAgICAgICAgICAgICAgICBjb21wW2ZpbltpXVtqXS5mXSA9IGk7CiAgICAgICAgICAgICAgICBjb21wW2ZpbltpXVtqXS5zXSA9IGk7CiAgICAgICAgICAgIH0KICAgICAgICAgfQogICAgfQp9OwoKQkNDIGI7CnZvaWQgYmZzKGludCBjb3csIGludCBib3gsIGludCBkaXIpewogICAgaWYodmlzW2JveF1bZGlyXSA9PSAxKXsKICAgICAgICByZXR1cm47CiAgICB9CiAgICAvL2JmcwogICAgbGlzdDxwaWk+IHE7CiAgICBxLnBiKG1wKGJveCwgY293KSk7CiAgICB2aXNbYm94XVtkaXJdID0gMTsKICAgIHdoaWxlKCFxLmVtcHR5KCkpewogICAgICAgIGNvdyA9IHEuZnJvbnQoKS5zOwogICAgICAgIGJveCA9IHEuZnJvbnQoKS5mOwogICAgICAgIHEucG9wX2Zyb250KCk7CiAgICAgICAgZm9yKGludCBpID0gMDsgaTw0OyBpKyspewogICAgICAgICAgICAvLyAwIGRvd24sIDEgcmlnaHQsIDIgdXAsIDMgbGVmdAogICAgICAgICAgICBpbnQgbnh0Q293ID0gdmFsaWQoYm94LCAoaSsyKSU0KTsgLy93aGVyZSBjb3cgaGFzIHRvIGJlIHRvIG1vdmUgYm94IGluIGRpcmVjdGlvbiBpCiAgICAgICAgICAgIGludCBueHRCb3ggPSB2YWxpZChib3gsIGkpOyAvL3RoZSBuZXh0IGxvY2F0aW9uIG9mIHRoZSBib3ggbW92ZWQgaW4gZGlyZWN0aW9uIGkKICAgICAgICAgICAgaWYobnh0Q293ID09IC0xIHx8IG54dEJveCA9PSAtMSl7IC8vY2hlY2tpbmcgaWYgdGhleSBhcmUgdmFsaWQKICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmKChiLmNvbXBbY293XSAhPSBiLmNvbXBbbnh0Q293XSkpeyAvL3NlZWluZyBpZiBpdCdzIHBvc3NpYmxlIGZvciB0aGUgY293IHRvIG1vdmUgdG8gdGhhdCBuZXcgbG9jYXRpb24KICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmKHZpc1tueHRCb3hdW2ldID09IDEpewogICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgLy9hZGRpbmcgdG8gcXVldWUKICAgICAgICAgICAgdmlzW254dEJveF1baV0gPSAxOwogICAgICAgICAgICBxLnB1c2hfYmFjayhtcChueHRCb3gsIGJveCkpOwoKICAgICAgICB9CiAgICB9Cn0KCnZvaWQgYmZzMShpbnQgY293LCBpbnQgYm94KXsvL2JmcyB0byBmaW5kIHdoZXJlIGNvdyBjYW4gYmUgYXJvdW5kIHRoZSBpbml0aWFsIHBvc2l0aW9uIG9mIGJveAogICAgbGlzdDxpbnQ+IHE7CiAgICBxLnBiKGNvdyk7CiAgICByZWFjaFtjb3ddID0xOwogICAgd2hpbGUoIXEuZW1wdHkoKSl7CiAgICAgICAgaW50IHMgPSBxLmZyb250KCk7CiAgICAgICAgcS5wb3BfZnJvbnQoKTsKICAgICAgICBmMHIoaSwgNCl7CiAgICAgICAgICAgIGludCBueHQgPSB2YWxpZChzLCBpKTsKICAgICAgICAgICAgaWYobnh0ICE9IC0xICYmIG54dCAgIT0gYm94ICYmIHJlYWNoW254dF0gPT0gMCl7CiAgICAgICAgICAgICAgICByZWFjaFtueHRdID0gIDE7CiAgICAgICAgICAgICAgICBxLnBiKG54dCk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0KaW50IG1haW4oKXsKICAgLy8gaW8oInB1c2hhYm94Iik7CiAgICBjaW4gPj5uID4+bSA+PiBxOwogICAgc3RyaW5nIHRlbXBvcmFyeTsKICAgIGdldGxpbmUoY2luLCB0ZW1wb3JhcnkpOwogICAgaW50IGNvdyA9IDA7CiAgICBpbnQgYm94ID0gMDsKICAgIC8vcmVhZCBpbnB1dAogICAgZjByKGksIG4pewogICAgICAgIHN0cmluZyBsaW5lOwogICAgICAgIGdldGxpbmUoY2luLCBsaW5lKTsKICAgICAgICBmMHIoaiwgbSl7CiAgICAgICAgICAgIGlmKGxpbmVbal0gPT0gJyMnKXsKICAgICAgICAgICAgICAgIGdyaWRbaV1bal0gPSAxOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmKGxpbmVbal0gPT0gJ0EnKXsKICAgICAgICAgICAgICAgIGNvdyA9IGkqbSArajsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZihsaW5lW2pdID09ICdCJyl7CiAgICAgICAgICAgICAgICBib3ggPSBpKm0rajsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIC8vZmluZCB0aGUgYmljb25uZWN0ZWQgY29tcG9uZW50cwogICAgYi5pbml0KG4qbSk7CiAgICBiLmJjYygpOwogICAgLy9maW5kcyB3aGVyZSB0aGUgY293IGNhbiBiZSBhcm91bmQgdGhlIGJveAogICAgYmZzMShjb3csIGJveCk7CiAgICBmMHIoaSwgNCl7CiAgICAgICAgaW50IG54dENvdyA9IHZhbGlkKGJveCwgaSk7CiAgICAgICAgaWYobnh0Q293ICE9IC0xICYmIHJlYWNoW254dENvd10gPT0gMSl7CiAgICAgICAgICAgIC8vYmZzIGZyb20gZWFjaCBsb2NhdGlvbiB0byBmaWd1cmUgd2hpY2ggcGxhY2VzIHlvdSBjYW4gdmlzaXQKICAgICAgICAgICAgYmZzKG54dENvdywgYm94LCAoaSsyKSU0KTsKICAgICAgICB9CiAgICB9CiAgICBmMHIoaSwgcSl7CiAgICAgICAgaW50IHIsIGM7CiAgICAgICAgY2luID4+IHIgPj4gYzsKICAgICAgICByLS07IGMtLTsKICAgICAgICBpbnQgbG9jID0gciptICsgYzsKICAgICAgICBpZih2aXNbbG9jXVswXSA9PSAxIHx8IHZpc1tsb2NdWzFdID09IDEgfHwgdmlzW2xvY11bMl0gPT0gMSB8fCB2aXNbbG9jXVszXSA9PSAxIHx8IGxvYyA9PSBib3gpewogICAgICAgICAgICBjb3V0IDw8ICJZRVMiIDw8IGVuZGw7CiAgICAgICAgfQogICAgICAgIGVsc2V7CiAgICAgICAgICAgIGNvdXQgPDwgIk5PIiA8PCBlbmRsOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9Cg==