#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, m, T;
int a[N][N];
int f[N][N];
bool vis[N][N];
int pos[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
char s[N];
pair<int, int> q[N * N];
bool isgood(int x, int y) {
  for (int i = 0; i < 4; i++) {
    int nx = x + pos[i][0];
    int ny = y + pos[i][1];
    if (a[nx][ny] == a[x][y]) {
      return true;
    }
  }
  return false;
}
void bfs() {
  int front = 0;
  int rear = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (isgood(i, j)) {
        f[i][j] = 0;
        vis[i][j] = true;
        q[rear++] = {i, j};
      }
    }
  }
  while (front < rear) {
    auto now = q[front++];
    for (int i = 0; i < 4; i++) {
      int nx = now.first + pos[i][0];
      int ny = now.second + pos[i][1];
      if (nx < 1 || nx > n || ny < 1 || ny > m || vis[nx][ny]) {
        continue;
      }
      f[nx][ny] = f[now.first][now.second] + 1;
      vis[nx][ny] = true;
      q[rear++] = {nx, ny};
    }
  }
}
int main() {
  cin >> n >> m >> T;
  memset(a, -1, sizeof(a));
  for (int i = 1; i <= n; i++) {
    cin >> (s + 1);
    for (int j = 1; j <= m; j++) {
      a[i][j] = s[j] - '0';
    }
  }
  bfs();
  int x, y;
  long long int t;
  while (T--) {
    cin >> x >> y >> t;
    if (vis[x][y]) {
      cout << ((a[x][y]) ^ (max(0LL, t - f[x][y]) & 1));
    } else {
      cout << a[x][y];
    }
    cout << endl;
  }
  return 0;
}
