#include <bits/stdc++.h>
using namespace std;
long long a[1005][1005];
long long t[1005][1005];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, T;
  cin >> n >> m >> T;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      char c;
      cin >> c;
      a[i][j] = (c == '1');
      t[i][j] = -1;
    }
  }
  queue<pair<int, int> > q;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      for (int k = 0; k < 4; ++k) {
        int x = i + dx[k];
        int y = j + dy[k];
        if (x < 1 || x > n || y < 1 || y > m) continue;
        if (a[x][y] == a[i][j]) {
          q.push(pair<int, int>(i, j));
          t[i][j] = 0;
          break;
        }
      }
    }
  }
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    q.pop();
    for (int k = 0; k < 4; ++k) {
      int x = i + dx[k];
      int y = j + dy[k];
      if (x < 1 || x > n || y < 1 || y > m) continue;
      if (t[x][y] == -1) {
        t[x][y] = t[i][j] + 1;
        q.push(pair<int, int>(x, y));
      }
    }
  }
  while (T--) {
    int x, y;
    long long p;
    cin >> x >> y >> p;
    if (t[x][y] == -1 || p < t[x][y])
      cout << a[x][y] << "\n";
    else {
      cout << ((a[x][y] + p - t[x][y]) % 2 + 2) % 2 << "\n";
    }
  }
}
