#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1010;
int n, m, t;
long long p;
int sq[MAX_N][MAX_N];
int firstChange[MAX_N][MAX_N];
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, 1, -1, 0};
bool inBounds(int i, int j) { return 0 <= i && i < n && 0 <= j && j < m; }
bool diffNeighbor(int i, int j) {
  if (inBounds(i - 1, j) && sq[i - 1][j] == sq[i][j]) return 1;
  if (inBounds(i + 1, j) && sq[i + 1][j] == sq[i][j]) return 1;
  if (inBounds(i, j - 1) && sq[i][j - 1] == sq[i][j]) return 1;
  if (inBounds(i, j + 1) && sq[i][j + 1] == sq[i][j]) return 1;
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> t;
  for (int i = 0; i < n; i++) {
    string second;
    cin >> second;
    for (int j = 0; j < m; j++) {
      if (second[j] == '0')
        sq[i][j] = 0;
      else
        sq[i][j] = 1;
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) firstChange[i][j] = -1;
  queue<pair<pair<int, int>, int>> q;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (diffNeighbor(i, j)) {
        q.push(make_pair(make_pair(i, j), 0));
      }
    }
  while (!q.empty()) {
    pair<pair<int, int>, int> val = q.front();
    q.pop();
    int ux = val.first.first;
    int uy = val.first.second;
    int d = val.second;
    if (firstChange[ux][uy] != -1) continue;
    firstChange[ux][uy] = d;
    for (int dir = 0; dir < 4; dir++) {
      int vx = ux + dx[dir];
      int vy = uy + dy[dir];
      if (inBounds(vx, vy)) {
        if (firstChange[vx][vy] == -1) {
          q.push(make_pair(make_pair(vx, vy), d + 1));
        }
      }
    }
  }
  while (t--) {
    int i, j;
    long long p;
    cin >> i >> j >> p;
    i--;
    j--;
    if (firstChange[i][j] == -1)
      cout << sq[i][j] << "\n";
    else {
      if (firstChange[i][j] > p) {
        cout << sq[i][j] << "\n";
      } else {
        long long cnt = p - firstChange[i][j];
        int x = cnt % 2;
        cout << (sq[i][j] ^ x) << "\n";
      }
    }
  }
}
