#include <bits/stdc++.h>   #pragma GCC optimize(2)   using namespace std;   const int INF = 0x3f3f3f3f;   const long long inf = 0x3f3f3f3f3f3f3f3f;   const int mod = 1e9 + 7;   const int maxn = 1e3 + 5;   int n, m, qq, s[maxn][maxn], f[maxn][maxn];   bool vis[maxn][maxn];   int a[4] = {0, 1, 0, -1};   int b[4] = {-1, 0, 1, 0};   bool judge(int x, int y) {     for (int i = 0; i <= 3; i++) {       int xx = x + a[i], yy = y + b[i];       if (xx <= 0 || xx > n || yy <= 0 || yy > m) continue;       if (s[xx][yy] == s[x][y]) return true;     }     return false;   }   void bfs() {     queue<pair<int, int> > q;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (judge(i, j)) {           vis[i][j] = 1;           f[i][j] = 0;           q.push({i, j});         }       }     }     while (!q.empty()) {       int x = q.front().first;       int y = q.front().second;       q.pop();       for (int i = 0; i <= 3; i++) {         int xx = x + a[i], yy = y + b[i];         if (xx <= 0 || xx > n || yy <= 0 || yy > m) continue;         if (vis[xx][yy]) continue;         f[xx][yy] = f[x][y] + 1;         vis[xx][yy] = 1;         q.push({xx, yy});       }     }   }   int main() {     scanf( %d%d%d , &n, &m, &qq);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         scanf( %1d , &s[i][j]);       }     }     bfs();     while (qq--) {       int x, y;       long long t;       scanf( %d%d%lld , &x, &y, &t);       if (vis[x][y]) {         if (t < f[x][y])           printf( %d n , s[x][y]);         else           printf( %d n , s[x][y] ^ ((t - f[x][y]) & 1));       } else {         printf( %d n , s[x][y]);       }     }   }   