#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 10;
const long long inf = 1e18 + 10;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};
char s[N][N];
long long d[N][N];
int main() {
  ios::sync_with_stdio(0);
  int n, m, t;
  cin >> n >> m >> t;
  for (int i = 1; i <= n; i++) cin >> (s[i] + 1);
  queue<pair<int, int> > q;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      d[i][j] = inf;
      for (int k = 0; k < 4; k++) {
        int x = i + dx[k], y = j + dy[k];
        if (s[i][j] == s[x][y]) {
          d[i][j] = 0;
          q.push({i, j});
          break;
        }
      }
    }
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int tx = x + dx[i], ty = y + dy[i];
      if (tx == 0 || tx > n || ty == 0 || ty > m) continue;
      if (d[tx][ty] == inf) d[tx][ty] = d[x][y] + 1, q.push({tx, ty});
    }
  }
  while (t--) {
    int x, y;
    long long z;
    cin >> x >> y >> z;
    if (d[x][y] > z)
      cout << s[x][y] << endl;
    else {
      if ((z - d[x][y]) % 2 == 1)
        cout << char('1' + '0' - s[x][y]) << endl;
      else
        cout << s[x][y] << endl;
    }
  }
}
