#include <bits/stdc++.h>
using namespace std;
template <typename... Args>
void db(Args... args) {}
const long long MAXN = 1010;
long long dx[4] = {-1, 0, 1, 0};
long long dy[4] = {0, 1, 0, -1};
long long N, M, T;
long long A[MAXN][MAXN];
long long mark[MAXN][MAXN];
long long mark2[MAXN][MAXN];
long long tim[MAXN][MAXN];
long long SZ[MAXN][MAXN];
long long mk[MAXN][MAXN];
long long contg;
void DFS1(long long x, long long y) {
  mark[x][y] = true;
  contg++;
  for (long long i = 0; i < 4; i++) {
    long long xx = x + dx[i];
    long long yy = y + dy[i];
    if (1 <= xx && xx <= N && 1 <= yy && yy <= M && !mark[xx][yy] &&
        A[x][y] == A[xx][yy]) {
      DFS1(xx, yy);
    }
  }
}
void DFS2(long long x, long long y) {
  mark2[x][y] = true;
  SZ[x][y] = contg;
  for (long long i = 0; i < 4; i++) {
    long long xx = x + dx[i];
    long long yy = y + dy[i];
    if (1 <= xx && xx <= N && 1 <= yy && yy <= M && !mark2[xx][yy] &&
        A[x][y] == A[xx][yy]) {
      DFS2(xx, yy);
    }
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> N >> M >> T;
  for (long long i = 1; i <= N; i++) {
    for (long long j = 1; j <= M; j++) {
      char c;
      cin >> c;
      if (c == '1')
        A[i][j] = 1;
      else
        A[i][j] = 0;
    }
  }
  for (long long i = 1; i <= N; i++) {
    for (long long j = 1; j <= M; j++) {
      if (!mark[i][j]) {
        contg = 0;
        DFS1(i, j);
        DFS2(i, j);
      }
    }
  }
  queue<pair<long long, long long>> Q;
  for (long long i = 1; i <= N; i++) {
    for (long long j = 1; j <= M; j++) {
      if (SZ[i][j] == 1) tim[i][j] = 2e18;
      for (long long d = 0; d < 4; d++) {
        long long xx = i + dx[d];
        long long yy = j + dy[d];
        if (1 <= xx && xx <= N && 1 <= yy && yy <= M && SZ[i][j] == 1 &&
            SZ[xx][yy] > 1) {
          Q.push({i, j});
          tim[i][j] = 1;
          mk[i][j] = true;
          break;
        }
      }
    }
  }
  while (Q.size()) {
    long long x = Q.front().first;
    long long y = Q.front().second;
    Q.pop();
    for (long long d = 0; d < 4; d++) {
      long long xx = x + dx[d];
      long long yy = y + dy[d];
      if (1 <= xx && xx <= N && 1 <= yy && yy <= M && SZ[xx][yy] == 1 &&
          !mk[xx][yy]) {
        Q.push({xx, yy});
        tim[xx][yy] = tim[x][y] + 1;
        mk[xx][yy] = true;
      }
    }
  }
  for (long long i = 1; i <= T; i++) {
    long long x, y, p;
    cin >> x >> y >> p;
    if (p < tim[x][y])
      cout << A[x][y] << '\n';
    else {
      if ((p - tim[x][y]) % 2 == 0) {
        cout << A[x][y] << '\n';
      } else {
        cout << !A[x][y] << '\n';
      }
    }
  }
  return 0;
}
