#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }
long long max3(long long a, long long b, long long c) {
  return max(max(a, b), c);
}
long long max4(long long a, long long b, long long c, long long d) {
  return max(max(a, b), max(c, d));
}
long long min3(long long a, long long b, long long c) {
  return min(min(a, b), c);
}
long long min4(long long a, long long b, long long c, long long d) {
  return min(min(a, b), min(c, d));
}
long long po(long long x, long long n, long long mod = 1000000007) {
  if (n == 0) {
    return 1;
  }
  long long m = po(x, n / 2, mod);
  m *= m;
  m %= mod;
  if (n % 2) {
    m *= x;
    m %= mod;
  }
  return m;
}
bool comp(pair<long long, long long> p1, pair<long long, long long> p2) {
  return p1.second <= p2.second;
}
long long n, m, t;
vector<long long> x{-1, 0, 1, 0};
vector<long long> y{0, 1, 0, -1};
char arr[1005][1005];
bool same(long long ii, long long jj) {
  for (long long k = 0; k < 4; k++) {
    long long i = ii + x[k];
    long long j = jj + y[k];
    if (i >= 0 && i < n && j >= 0 && j < m && arr[i][j] == arr[ii][jj]) {
      return 1;
    }
  }
  return 0;
}
int32_t main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  clock_t startTime = clock();
  cout << fixed << setprecision(12);
  cin >> n >> m >> t;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      cin >> arr[i][j];
    }
  }
  long long a[n][m];
  bool vis[n][m];
  memset(vis, 0, sizeof(vis));
  queue<pair<long long, long long> > q;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      if (same(i, j)) {
        q.push({i, j});
        a[i][j] = 0;
        vis[i][j] = 1;
      } else {
        a[i][j] = LLONG_MAX;
      }
    }
  }
  while (!q.empty()) {
    pair<long long, long long> p = q.front();
    q.pop();
    long long ii = p.first, jj = p.second;
    for (long long k = 0; k < 4; k++) {
      long long i = ii + x[k];
      long long j = jj + y[k];
      if (i >= 0 && i < n && j >= 0 && j < m && !vis[i][j]) {
        vis[i][j] = 1;
        a[i][j] = a[ii][jj] + 1;
        q.push({i, j});
      }
    }
  }
  while (t--) {
    long long i, j, f;
    cin >> i >> j >> f;
    i--;
    j--;
    if (a[i][j] > f) {
      cout << arr[i][j] << "\n";
    } else {
      long long x = f - a[i][j];
      long long k = arr[i][j] - '0';
      if (x % 2 == 0) {
        cout << k << "\n";
      } else {
        cout << (k ^ 1) << "\n";
      }
    }
  }
}
