#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
long long dis[maxn][maxn];
char grid[maxn][maxn];
int row, col, x, y, t;
int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
long long p;
int main() {
  cin >> row >> col >> t;
  for (int i = 1; i <= row; i++) scanf("%s", grid[i] + 1);
  queue<pair<int, int> > q;
  for (int i = 1; i <= row; i++) {
    for (int j = 1; j <= col; j++) {
      char ch = grid[i][j];
      dis[i][j] = 1e18 + 10000;
      if (ch == grid[i + 1][j] || ch == grid[i - 1][j] ||
          ch == grid[i][j - 1] || ch == grid[i][j + 1])
        dis[i][j] = 1, q.push({i, j});
    }
  }
  while (!q.empty()) {
    x = q.front().first, y = q.front().second, q.pop();
    for (int i = 0; i < 4; i++) {
      int xx = x + dir[i][0], yy = y + dir[i][1];
      if (xx < 1 || xx > row || yy > col || yy < 1) continue;
      if (dis[xx][yy] > dis[x][y] + 1) {
        dis[xx][yy] = dis[x][y] + 1;
        q.push({xx, yy});
      }
    }
  }
  while (t--) {
    cin >> x >> y >> p;
    if (dis[x][y] > p)
      cout << grid[x][y] << endl;
    else {
      int value = dis[x][y] - p;
      value = -value;
      if (value & 1)
        cout << grid[x][y] << endl;
      else
        cout << ((grid[x][y] - '0') ^ 1) << endl;
    }
  }
  return 0;
}
