#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
const int M = 1000000007;
const long double PI = acos(-1);
int n, m, t, l, c;
char x;
long long p;
int good[N][N], aux[N][N];
bool a[N][N];
int dl[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};
queue<pair<int, int> > q;
bool inline inside(pair<int, int> v) {
  return v.first >= 1 && v.first <= n && v.second >= 1 && v.second <= m;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int i, j;
  cin >> n >> m >> t;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      cin >> x;
      a[i][j] = x - '0';
    }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      if (a[i][j] == a[i - 1][j] && i > 1) {
        good[i][j] = aux[i][j] = 1;
        q.push({i, j});
        continue;
      }
      if (a[i][j] == a[i][j + 1] && j < m) {
        good[i][j] = aux[i][j] = 1;
        q.push({i, j});
        continue;
      }
      if (a[i][j] == a[i + 1][j] && i < n) {
        good[i][j] = aux[i][j] = 1;
        q.push({i, j});
        continue;
      }
      if (a[i][j] == a[i][j - 1] && j > 1) {
        good[i][j] = aux[i][j] = 1;
        q.push({i, j});
        continue;
      }
    }
  pair<int, int> pf, v;
  while (!q.empty()) {
    pf = q.front();
    q.pop();
    for (i = 0; i < 4; i++) {
      v.first = pf.first + dl[i];
      v.second = pf.second + dc[i];
      if (inside(v) && !aux[v.first][v.second]) {
        aux[v.first][v.second] = 1 + aux[pf.first][pf.second];
        good[v.first][v.second] = aux[v.first][v.second];
        q.push(v);
      }
    }
  }
  for (i = 1; i <= t; i++) {
    cin >> l >> c >> p;
    if (good[l][c] == 0)
      cout << a[l][c] << '\n';
    else {
      if (good[l][c] > p)
        cout << a[l][c] << '\n';
      else {
        if (good[l][c] % 2 == p % 2)
          cout << 1 - a[l][c] << '\n';
        else
          cout << a[l][c] << '\n';
      }
    }
  }
  return 0;
}
