#include <bits/stdc++.h>
using namespace std;
int tab[1007][1007];
bool odw[1007][1007];
int kiedy[1007][1007];
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
               greater<pair<int, pair<int, int>>>>
    q;
int n, m;
void dijkstra() {
  while (!q.empty()) {
    int x = q.top().second.first;
    int y = q.top().second.second;
    int z = q.top().first;
    q.pop();
    if (odw[x][y]) continue;
    odw[x][y] = 1;
    kiedy[x][y] = z;
    if (x > 1) {
      q.push({z + 1, {x - 1, y}});
    }
    if (x < n) {
      q.push({z + 1, {x + 1, y}});
    }
    if (y > 1) {
      q.push({z + 1, {x, y - 1}});
    }
    if (y < m) {
      q.push({z + 1, {x, y + 1}});
    }
  }
}
int main(void) {
  int t;
  char ch;
  scanf("%d %d %d", &n, &m, &t);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf(" %c", &ch);
      if (ch == '1') {
        tab[i][j] = 1;
      } else {
        tab[i][j] = 0;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (i > 1) {
        if (tab[i][j] == tab[i - 1][j]) {
          q.push({0, {i, j}});
          continue;
        }
      }
      if (i < n) {
        if (tab[i][j] == tab[i + 1][j]) {
          q.push({0, {i, j}});
          continue;
        }
      }
      if (j > 1) {
        if (tab[i][j] == tab[i][j - 1]) {
          q.push({0, {i, j}});
          continue;
        }
      }
      if (j < m) {
        if (tab[i][j] == tab[i][j + 1]) {
          q.push({0, {i, j}});
          continue;
        }
      }
    }
  }
  dijkstra();
  int a, b;
  long long c;
  for (int i = 1; i <= t; i++) {
    scanf("%d %d %lld", &a, &b, &c);
    if (kiedy[a][b] >= c || !odw[a][b]) {
      printf("%d\n", tab[a][b]);
    } else {
      if ((c - kiedy[a][b]) % 2 == 0) {
        printf("%d\n", tab[a][b]);
      } else {
        printf("%d\n", tab[a][b] ^ 1);
      }
    }
  }
  return 0;
}
