#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> d = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
void bfs(vector<vector<int>> arr, vector<vector<long long>> &dist, int n,
         int m) {
  queue<pair<int, int>> q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < 4; k++) {
        int x = i + d[k].first, y = j + d[k].second;
        if (x < 0 || y < 0 || x == n || y == m || arr[i][j] != arr[x][y])
          continue;
        else {
          dist[i][j] = 0;
          q.push({i, j});
          break;
        }
      }
    }
  }
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int xx = x + d[i].first, yy = y + d[i].second;
      if (xx < 0 || yy < 0 || xx == n || yy == m ||
          dist[x][y] + 1 >= dist[xx][yy])
        continue;
      dist[xx][yy] = dist[x][y] + 1;
      q.push({xx, yy});
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, t;
  ;
  cin >> n >> m >> t;
  vector<vector<int>> arr(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      if (s[j] == '0')
        arr[i][j] = 0;
      else
        arr[i][j] = 1;
    }
  }
  vector<vector<long long>> dist(n, vector<long long>(m, LLONG_MAX));
  bfs(arr, dist, n, m);
  while (t--) {
    int i, j;
    long long p;
    cin >> i >> j >> p;
    if (p <= dist[i - 1][j - 1])
      cout << arr[i - 1][j - 1] << '\n';
    else {
      int ans = ((p - dist[i - 1][j - 1]) % 2) ^ arr[i - 1][j - 1];
      cout << ans << '\n';
    }
  }
}
