#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e3 + 10;
int n, m, t;
char mt[N][N];
int change[N][N];
vector<vector<int>> mx;
int di[] = {-1, 1, 0, 0};
int dj[] = {0, 0, -1, 1};
bool is_valid(int i, int j) { return i >= 0 and i < n and j >= 0 and j < m; }
int main() {
  scanf("%d %d %d", &n, &m, &t);
  for (int i = 0; i < n; ++i) scanf("%s", mt[i]);
  queue<pair<int, int>> q;
  memset(change, -1, sizeof change);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      for (int k = 0; k < 4; ++k) {
        int pi = i + di[k];
        int pj = j + dj[k];
        if (is_valid(pi, pj)) {
          if (mt[i][j] == mt[pi][pj]) {
            change[i][j] = 0;
            q.emplace(i, j);
            break;
          }
        }
      }
    }
  }
  while (!q.empty()) {
    auto cur = q.front();
    q.pop();
    for (int k = 0; k < 4; ++k) {
      int pi = cur.first + di[k];
      int pj = cur.second + dj[k];
      if (is_valid(pi, pj) and change[pi][pj] == -1) {
        change[pi][pj] = change[cur.first][cur.second] + 1;
        q.emplace(pi, pj);
      }
    }
  }
  while (t-- > 0) {
    int i, j;
    ll p;
    scanf("%d %d %lld", &i, &j, &p);
    i--, j--;
    if (change[i][j] == -1 or change[i][j] >= p)
      printf("%c\n", mt[i][j]);
    else {
      ll diff = p - change[i][j];
      if (diff & 1)
        printf("%c\n", mt[i][j] == '1' ? '0' : '1');
      else
        printf("%c\n", mt[i][j]);
    }
  }
  return 0;
}
