func_code_string: #include <bits/stdc++.h>   using namespace std;   const long long INF = LLONG_MAX;   const int mxn = 1011;   bool DEBUG = 0;   string s[mxn];   bool grid[mxn][mxn], chg[mxn][mxn];   long long dist[mxn][mxn];   const int dx[] = {-1, 0, 1, 0};   const int dy[] = {0, -1, 0, 1};   int n, m;   bool check(int x, int y) {     if (0 <= x && x < n && 0 <= y && y < m) return 1;     return 0;   }   void init() {     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (chg[i][j]) continue;         bool f = 0;         for (int k = 0; k < 4 && !f; k++) {           int x = i + dx[k];           int y = j + dy[k];           if (!check(x, y)) continue;           if (grid[x][y] == grid[i][j]) {             f = 1;             chg[x][y] = 1;           }         }         if (f)           chg[i][j] = 1;         else           chg[i][j] = 0;       }     }   }   void bfs(int x, int y) {     queue<pair<int, int> > q;     q.push(pair<int, int>(x, y));     while (!q.empty()) {       pair<int, int> now = q.front();       x = now.first;       y = now.second;       q.pop();       for (int i = 0; i < 4; i++) {         int X = x + dx[i];         int Y = y + dy[i];         if (!check(X, Y)) continue;         if (dist[x][y] + 1 < dist[X][Y]) {           dist[X][Y] = dist[x][y] + 1;           q.push(pair<int, int>(X, Y));         }       }     }     return;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int q;     cin >> n >> m >> q;     for (int i = 0; i < n; i++) {       cin >> s[i];       for (int j = 0; j < m; j++) {         grid[i][j] = s[i][j] ==  1 ;       }     }     init();     vector<pair<int, int> > visit;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (chg[i][j]) {           dist[i][j] = 0;           visit.emplace_back(i, j);         } else {           dist[i][j] = 1e18 + 1;         }       }     }     for (auto cor : visit) {       bfs(cor.first, cor.second);     }     int a, b;     long long t;     while (q--) {       cin >> a >> b >> t;       a--;       b--;       if (t < dist[a][b]) {         cout << grid[a][b] <<   n ;       } else {         t -= dist[a][b];         if (t % 2)           cout << int(grid[a][b] ^ 1) <<   n ;         else           cout << grid[a][b] <<   n ;       }     }   }   