func_code_string: #include <bits/stdc++.h>   using namespace std;   string s[1005];   long long int M[1005][1005], T[1005][1005];   queue<pair<long long int, long long int> > q;   long long int xr[] = {0, 0, -1, 1};   long long int xc[] = {-1, 1, 0, 0};   int main() {     ios_base::sync_with_stdio(false);     cout.tie(0);     cin.tie(0);     long long int p, t, n, m, i, j, k, x, y, z, a, b, c;     cin >> n >> m >> t;     for (i = 0; i < n; ++i) cin >> s[i];     for (i = 0; i < n; ++i) {       for (j = 0; j < m; ++j) {         a = 0;         for (k = 0; k < 4; ++k) {           x = i + xr[k];           y = j + xc[k];           if (x < 0 || x >= n || y < 0 || y >= m) continue;           a += (s[x][y] == s[i][j]);         }         if (a) {           q.push(make_pair(i, j));           M[i][j] = 1;         } else           T[i][j] = LLONG_MAX;       }     }     while (!q.empty()) {       auto it = q.front();       q.pop();       i = it.first;       j = it.second;       a = b = c = 0;       for (k = 0; k < 4; ++k) {         x = i + xr[k];         y = j + xc[k];         if (x < 0 || x >= n || y < 0 || y >= m || M[x][y]) continue;         if (s[x][y] != s[i][j]) {           q.push(make_pair(x, y));           M[x][y] = 1;           T[x][y] = T[i][j] + 1;         }       }     }     while (t--) {       cin >> i >> j >> p;       i -= 1;       j -= 1;       if (T[i][j] > p)         cout << s[i][j] << endl;       else         cout << (((p - T[i][j]) & 1) ^ (s[i][j] -  0 )) << endl;     }     return 0;   }   