#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int b, long long int m) {
  long long int ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}
long long int n, m;
long long int x[4] = {+1, -1, 0, 0};
long long int y[4] = {0, 0, +1, -1};
bool vis[1002][1002] = {0};
long long int dist[1002][1002] = {0};
long long int a[1002][1002];
bool good[1002][1002] = {0};
bool check(long long int xx, long long int yy) {
  if (xx >= 1 and xx <= n and yy >= 1 and yy <= m) return 1;
  return 0;
}
vector<pair<long long int, long long int> > sp;
void bfs() {
  queue<pair<long long int, long long int> > q;
  for (long long int i = 0; i < sp.size(); i++) {
    long long int xx = sp[i].first;
    long long int yy = sp[i].second;
    vis[xx][yy] = 1;
    for (long long int j = 0; j < 4; j++) {
      if (!check(xx + x[j], yy + y[j])) continue;
      if (vis[xx + x[j]][yy + y[j]]) continue;
      if (good[xx + x[j]][yy + y[j]]) continue;
      vis[xx + x[j]][yy + y[j]] = 1;
      q.push({xx + x[j], yy + y[j]});
      dist[xx + x[j]][yy + y[j]] = 1;
    }
  }
  while (!q.empty()) {
    pair<long long int, long long int> g = q.front();
    q.pop();
    long long int xx = g.first, yy = g.second;
    for (long long int j = 0; j < 4; j++) {
      if (!check(xx + x[j], yy + y[j])) continue;
      if (vis[xx + x[j]][yy + y[j]]) continue;
      if (good[xx + x[j]][yy + y[j]]) continue;
      vis[xx + x[j]][yy + y[j]] = 1;
      q.push({xx + x[j], yy + y[j]});
      dist[xx + x[j]][yy + y[j]] = dist[xx][yy] + 1;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  long long int q;
  cin >> q;
  for (long long int i = 1; i < n + 1; i++) {
    string s;
    cin >> s;
    for (long long int j = 1; j < m + 1; j++) a[i][j] = s[j - 1] - '0';
  }
  for (long long int i = 1; i < n + 1; i++) {
    for (long long int j = 1; j < m + 1; j++) {
      for (long long int k = 0; k < 4; k++) {
        long long int xx = i + x[k];
        long long int yy = j + y[k];
        if (!check(xx, yy)) continue;
        if (a[xx][yy] == a[i][j]) good[i][j] = 1;
      }
    }
  }
  for (long long int i = 1; i < n + 1; i++) {
    for (long long int j = 1; j < m + 1; j++) {
      dist[i][j] = 1000000007;
      if (good[i][j]) sp.push_back({i, j});
    }
  }
  bfs();
  while (q--) {
    long long int l, r;
    cin >> l >> r;
    long long int t;
    cin >> t;
    if (good[l][r]) {
      if (t & 1)
        cout << 1 - a[l][r];
      else
        cout << a[l][r];
      cout << endl;
    } else {
      long long int distance = dist[l][r];
      if (distance == 1000000007) {
        cout << a[l][r] << endl;
        continue;
      }
      if (t <= distance)
        cout << a[l][r] << endl;
      else {
        t -= distance;
        if (t & 1)
          cout << 1 - a[l][r] << endl;
        else
          cout << a[l][r] << endl;
      }
    }
  }
}
