#include <bits/stdc++.h>
using namespace std;
const int maxn = 1001;
bool mat[maxn][maxn];
int dist[maxn][maxn], n, m, tt;
pair<int, int> dir[4] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> tt;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      mat[i][j] = (c == '1');
    }
  queue<pair<int, int>> q;
  memset(dist, -1, sizeof(dist));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      int ones = 0, zeroes = 0;
      for (pair<int, int> d : dir) {
        int r = i + d.first, c = j + d.second;
        if (r >= 0 && r < n && c >= 0 && c < m) {
          ones += mat[r][c];
          zeroes += !mat[r][c];
        }
      }
      if (mat[i][j] && ones >= 1) {
        dist[i][j] = 0;
        q.push({i, j});
      } else if (!mat[i][j] && zeroes >= 1) {
        dist[i][j] = 0;
        q.push({i, j});
      }
    }
  while (!q.empty()) {
    int r = q.front().first, c = q.front().second;
    q.pop();
    for (pair<int, int> d : dir) {
      int rr = r + d.first, cc = c + d.second;
      if (rr >= 0 && rr < n && cc >= 0 && cc < m && dist[rr][cc] == -1) {
        dist[rr][cc] = dist[r][c] + 1;
        q.push({rr, cc});
      }
    }
  }
  while (tt--) {
    int r, c;
    long long p;
    cin >> r >> c >> p;
    r--;
    c--;
    if (dist[r][c] == -1) {
      cout << mat[r][c] << "\n";
    } else {
      if (p <= dist[r][c]) {
        cout << mat[r][c] << "\n";
      } else {
        long long steps = p - dist[r][c];
        if ((steps & 1) == 0) {
          cout << mat[r][c] << "\n";
        } else {
          cout << !mat[r][c] << "\n";
        }
      }
    }
  }
  return 0;
}
