#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse4 )   using namespace std;   using ll = long long;   using vi = vector<int>;   using pii = pair<int, int>;   const int MX = 1e5 + 10;   template <typename T>   istream &operator>>(istream &is, vector<T> &v) {     for (auto i = begin(v); i != end(v); i++) is >> *i;     return is;   }   template <typename T>   ostream &operator<<(ostream &os, const vector<T> &v) {     for (auto i = begin(v); i != end(v); i++)       os << *i << (i == end(v) - 1 ?   n  :    );     return os;   }   template <typename T>   inline void rd(T &s) {     s = 0;     T t = 1, k = getchar();     for (; k <  0  || k >  9 ; k = getchar())       if (k ==  - ) t = -1;     for (; k >=  0  && k <=  9 ; k = getchar())       s = (s << 1) + (s << 3) + (k ^ 48);     s *= t;   }   ll n, m, t;   char a[1111][1111];   ll b[1111][1111];   ll dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};   void solve() {     cin >> n >> m >> t;     for (register ll i = 1, ilen = (ll)(n); i <= ilen; ++i) cin >> (a[i] + 1);     queue<pii> q;     for (register ll i = 1, ilen = (ll)(n); i <= ilen; ++i)       for (register ll j = 1, jlen = (ll)(m); j <= jlen; ++j) {         if (a[i][j] == a[i + 1][j] || a[i][j] == a[i - 1][j] ||             a[i][j] == a[i][j + 1] || a[i][j] == a[i][j - 1])           b[i][j] = 1, q.push(make_pair(i, j));         else           b[i][j] = 2e18;       }     while (q.size()) {       ll x = q.front().first, y = q.front().second;       q.pop();       for (register ll i = 0, ilen = (ll)(4); i < ilen; ++i) {         ll nx = x + dx[i], ny = y + dy[i];         if (nx >= 1 && nx <= n && ny >= 1 && ny <= m) {           if (b[x][y] + 1 < b[nx][ny]) q.push(make_pair(nx, ny));           b[nx][ny] = min(b[nx][ny], b[x][y] + 1);         }       }     }     while (t--) {       ll x, y, p;       cin >> x >> y >> p;       if (p >= b[x][y] && (p - b[x][y]) % 2 == 0) {         if (a[x][y] ==  1 )           cout <<  0  <<   n ;         else           cout <<  1  <<   n ;       } else         cout << a[x][y] <<   n ;     }   }   signed main() {     ll T = 1;     while (T--) {       solve();     }     return 0;   }   