func_code_string: #include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   const int M = 1000005;   const int nmax = 500010;   const int MOD = (1e9) + 7;   int nextX[] = {1, -1, 0, 0};   int nextY[] = {0, 0, 1, -1};   int n, m, t;   int f[1010][1010];   char s[1010][1010];   bool good(int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= m); }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n >> m >> t;     for (int i = 1; i <= n; i++) cin >> s[i] + 1;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) f[i][j] = -1;     queue<pair<int, int> > que;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         int nr = 0;         for (int k = 0; k < 4; k++) {           int x = i + nextX[k];           int y = j + nextY[k];           if (!good(x, y)) continue;           if (s[i][j] == s[x][y]) nr++;         }         if (nr > 0) {           que.push({i, j});           f[i][j] = 0;         }       }     while (!que.empty()) {       int x = que.front().first;       int y = que.front().second;       que.pop();       for (int k = 0; k < 4; k++) {         int nxt_x = x + nextX[k];         int nxt_y = y + nextY[k];         if (!good(nxt_x, nxt_y)) continue;         if (s[nxt_x][nxt_y] != s[x][y] && f[nxt_x][nxt_y] == -1) {           f[nxt_x][nxt_y] = f[x][y] + 1;           que.push({nxt_x, nxt_y});         }       }     }     for (int i = 1; i <= t; i++) {       int x, y;       long long int p;       cin >> x >> y >> p;       if (f[x][y] == -1 || p < f[x][y])         cout << s[x][y] <<   n ;       else {         long long int diff = p - f[x][y];         if (diff % 2 == 0)           cout << s[x][y] <<   n ;         else if (s[x][y] ==  1 )           cout <<  0 n ;         else           cout <<  1 n ;       }     }     return 0;   }   