#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 3;
const int M = 1000000007;
int arr[N][N], n, m, q;
;
long long dis[N][N];
bool vis[N][N];
int x_var[] = {0, 0, 1, -1};
int y_var[] = {1, -1, 0, 0};
bool is_valid(int i, int j) {
  if (i < 0 || j < 0 || i >= n || j >= m) {
    return false;
  }
  return true;
}
void solve() {
  scanf("%d", &n);
  scanf("%d", &m);
  scanf("%d", &q);
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      arr[i][j] = s[j] - '0';
      dis[i][j] = LONG_LONG_MAX;
    }
  }
  queue<pair<int, int>> Q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int var = 0; var < 4; var++) {
        int x2 = i + x_var[var];
        int y2 = j + y_var[var];
        if (is_valid(x2, y2) && arr[x2][y2] == arr[i][j]) {
          dis[i][j] = 1;
          Q.push({i, j});
          vis[i][j] = true;
          break;
        }
      }
    }
  }
  while (!Q.empty()) {
    pair<int, int> tp = Q.front();
    Q.pop();
    int x1 = tp.first;
    int y1 = tp.second;
    for (int var = 0; var < 4; var++) {
      int x2 = x1 + x_var[var];
      int y2 = y1 + y_var[var];
      if (is_valid(x2, y2) && !vis[x2][y2] && arr[x2][y2] != arr[x1][y1]) {
        dis[x2][y2] = dis[x1][y1] + 1;
        vis[x2][y2] = true;
        Q.push({x2, y2});
      }
    }
  }
  while (q--) {
    int x, y;
    scanf("%d", &x);
    scanf("%d", &y);
    --x;
    --y;
    long long p;
    scanf("%lld", &p);
    long long dis1 = dis[x][y];
    if (p < dis1) {
      cout << arr[x][y] << endl;
    } else {
      long long m = (p - (dis1 - 1)) % 2;
      if (m == 1) {
        cout << 1 - arr[x][y] << endl;
      } else {
        cout << arr[x][y] << endl;
      }
    }
  }
}
int main() {
  solve();
  return 0;
}
