#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const int N = 1e6 + 5;   const int inf = 1 << 30;   const long long llf = 9e18;   long long powmod(long long a, long long b) {     long long res = 1;     a %= mod;     assert(b >= 0);     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   struct node {     int x, y, time;     friend bool operator<(const node &a, const node &b) {       return a.time > b.time;     }   };   bool cmp(int x, int y) { return x < y; }   int pic[1010][1010];   int book[1010][1010];   int dx[4] = {0, 0, 1, -1};   int dy[4] = {1, -1, 0, 0};   int n, m, t;   bool check(int x, int y) {     bool f1 = false;     bool f2 = false;     for (register int i = 0; i <= 3; ++i) {       if (x + dx[i] < 0 || x + dx[i] > n - 1 || y + dy[i] < 0 ||           y + dy[i] > m - 1)         continue;       if (pic[x + dx[i]][y + dy[i]] != pic[x][y]) {         f2 = true;       } else {         f1 = true;         book[x][y] = 0;       }     }     if (f1 && !f2) return false;     if (f1 && f2) return true;     if (!f1 && f2) return false;   }   int main() {     cin >> n >> m >> t;     string s;     for (register int i = 0; i <= n - 1; ++i) {       cin >> s;       for (register int j = 0; j <= m - 1; ++j) {         pic[i][j] = s[j] -  0 ;       }     }     node now, re;     priority_queue<node> q;     for (register int i = 0; i <= n - 1; ++i) {       for (register int j = 0; j <= m - 1; ++j) {         book[i][j] = inf;         if (check(i, j)) {           now.x = i;           now.y = j;           now.time = 0;           q.push(now);         }       }     }     while (!q.empty()) {       re = q.top();       q.pop();       int x = re.x, y = re.y;       for (register int i = 0; i <= 3; ++i) {         if (x + dx[i] < 0 || x + dx[i] > n - 1 || y + dy[i] < 0 ||             y + dy[i] > m - 1)           continue;         if (pic[x + dx[i]][y + dy[i]] != pic[x][y]) {           now.x = x + dx[i];           now.y = y + dy[i];           now.time = re.time + 1;           if (book[now.x][now.y] > now.time) {             book[now.x][now.y] = now.time;             q.push(now);           }         }       }     }     long long x, y, p;     for (register int i = 1; i <= t; ++i) {       cin >> x >> y >> p;       x--;       y--;       if (p < book[x][y] || book[x][y] == inf)         cout << pic[x][y] << endl;       else         cout << (pic[x][y] + (p - 1ll * book[x][y]) % 2) % 2 << endl;     }   }   