#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
long long int Lpow(long long int a, long long int b) {
  long long int ans = 1;
  while (b > 0) {
    if (b % 2) ans = (ans * a);
    a = (a * a);
    b = b / 2;
  }
  return ans;
}
long long int Lpow(long long int a, long long int b, long long int M) {
  long long int ans = 1;
  while (b > 0) {
    if (b % 2) ans = (ans * a) % M;
    a = (a * a) % M;
    b = b / 2;
  }
  return ans;
}
int arr[1005][1005];
int dist[1005][1005];
int in[1005][1005];
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 1; i <= n; i++) {
    string temp;
    cin >> temp;
    for (int j = 1; j <= m; j++) {
      arr[i][j] = int(temp[j - 1] - '0');
      dist[i][j] = 2147483647;
    }
  }
  queue<pair<int, int>> Q;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (i > 1 && arr[i - 1][j] == arr[i][j] && in[i][j] != 1)
        Q.push({i, j}), in[i][j] = 1, dist[i][j] = 0;
      if (j > 1 && arr[i][j - 1] == arr[i][j] && in[i][j] != 1)
        Q.push({i, j}), in[i][j] = 1, dist[i][j] = 0;
      if (i < n && arr[i + 1][j] == arr[i][j] && in[i][j] != 1)
        Q.push({i, j}), in[i][j] = 1, dist[i][j] = 0;
      if (j < m && arr[i][j + 1] == arr[i][j] && in[i][j] != 1)
        Q.push({i, j}), in[i][j] = 1, dist[i][j] = 0;
    }
  }
  while (!Q.empty()) {
    auto tmp = Q.front();
    int i = tmp.first;
    int j = tmp.second;
    Q.pop();
    in[i][j] = 0;
    if (i > 1 && dist[i - 1][j] > dist[i][j] + 1) {
      if (in[i - 1][j] != 1) Q.push({i - 1, j}), in[i - 1][j] = 1;
      dist[i - 1][j] = min(dist[i - 1][j], dist[i][j] + 1);
    }
    if (j > 1 && dist[i][j - 1] > dist[i][j] + 1) {
      if (in[i][j - 1] != 1) Q.push({i, j - 1}), in[i][j - 1] = 1;
      dist[i][j - 1] = min(dist[i][j - 1], dist[i][j] + 1);
    }
    if (i < n && dist[i + 1][j] > dist[i][j] + 1) {
      if (in[i + 1][j] != 1) Q.push({i + 1, j}), in[i + 1][j] = 1;
      dist[i + 1][j] = min(dist[i + 1][j], dist[i][j] + 1);
    }
    if (j < m && dist[i][j + 1] > dist[i][j] + 1) {
      if (in[i][j + 1] != 1) Q.push({i, j + 1}), in[i][j + 1] = 1;
      dist[i][j + 1] = min(dist[i][j + 1], dist[i][j] + 1);
    }
  }
  while (q--) {
    long long int x, y, k;
    cin >> x >> y >> k;
    if (dist[x][y] == 2147483647)
      cout << arr[x][y] << '\n';
    else if (max(0ll, k - dist[x][y]) % 2)
      cout << (arr[x][y] ^ 1) << '\n';
    else
      cout << arr[x][y] << '\n';
  }
  return 0;
}
