#include <bits/stdc++.h>
using namespace std;
struct hash_pair {
  template <class T1, class T2>
  size_t operator()(const pair<T1, T2>& p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};
long long t;
long long arr[1005][1005], dis[1003][1004];
string s[1001];
queue<pair<long long, long long> > q;
vector<long long> d1 = {1, -1, 0, 0};
vector<long long> d2 = {0, 0, 1, -1};
void bfs(long long a, long long b) {
  long long c, d, i, j;
  while (!q.empty()) {
    c = q.front().first;
    d = q.front().second;
    q.pop();
    for (i = 0; i < 4; i++) {
      long long f = c + d1[i];
      long long g = d + d2[i];
      if (f >= a || f < 0 || g >= b || g < 0) continue;
      if (arr[f][g]) continue;
      arr[f][g] = 1;
      dis[f][g] = dis[c][d] + 1;
      q.push({f, g});
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long a, b, c, d, e, f, g, i, j;
  long long n, k;
  cin >> a >> b >> t;
  for (i = 0; i < a; i++) {
    cin >> s[i];
  }
  for (i = 0; i < a; i++) {
    for (j = 0; j < b; j++) {
      for (e = 0; e < 4; e++) {
        c = i + d1[e];
        d = j + d2[e];
        if (c < a && c >= 0 && d < b && d >= 0) {
          if (s[i][j] == s[c][d]) {
            q.push({i, j});
            arr[i][j] = 1;
            break;
          }
        }
      }
    }
  }
  bfs(a, b);
  while (t--) {
    cin >> c >> d >> k;
    --c, --d;
    f = s[c][d] - '0';
    if (arr[c][d] == 0)
      cout << f << endl;
    else if (dis[c][d] == 0) {
      f += k;
      f = f % 2;
      cout << f << endl;
    } else {
      k -= dis[c][d];
      if (k <= 0) {
        cout << f << endl;
      } else {
        f += k;
        f %= 2;
        cout << f << endl;
      }
    }
  }
  return 0;
}
