#include <bits/stdc++.h>
using namespace std;
long long A[1009][1009], B[1009][1009], f[1009][1009], a[1009][1009];
long long dx[4] = {1, -1, 0, 0};
long long dy[4] = {0, 0, 1, -1};
int main() {
  ios_base::sync_with_stdio(false);
  long long n, m, t;
  cin >> n >> m >> t;
  for (long long i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (long long j = 1; j <= m; j++) {
      a[i][j] = s[j - 1] - '0';
    }
  }
  queue<pair<long long, long long> > q;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      f[i][j] = 1;
    }
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      A[i][j] = 1e9;
      for (long long d = 0; d < 4; d++) {
        long long nx = i + dx[d];
        long long ny = j + dy[d];
        if (f[nx][ny] && a[i][j] == a[nx][ny]) {
          q.push({i, j});
          A[i][j] = 1;
          break;
        }
      }
    }
  }
  while (q.size()) {
    long long x = q.front().first, y = q.front().second;
    q.pop();
    for (long long i = 0; i < 4; i++) {
      long long nx = x + dx[i];
      long long ny = y + dy[i];
      if (A[nx][ny] == 1e9 && f[nx][ny]) {
        A[nx][ny] = A[x][y] + 1;
        q.push({nx, ny});
      }
    }
  }
  while (t--) {
    long long i, j, p;
    cin >> i >> j >> p;
    long long x = A[i][j];
    if (x == 1e9 || x > p) {
      cout << a[i][j] << endl;
    } else {
      if ((p - x) % 2 == 0) {
        cout << 1 - a[i][j] << endl;
      } else
        cout << a[i][j] << endl;
    }
  }
}
