#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
void printV(vector<T> A) {
  if (A.empty())
    cerr << "EMTPY\n";
  else {
    cerr << "[";
    for (int i = 0; i < A.size() - 1; ++i) {
      cerr << A[i] << ", ";
    }
    if (A.size()) cerr << A.back() << "]\n";
  }
}
template <typename T>
void printVV(vector<vector<T>> A) {
  cerr << "[";
  for (auto v : A) {
    printV<T>(v);
  }
  cerr << "]";
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
int n, m, t;
vector<string> arr;
int dirs[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
const int N = 1001;
int when[N][N];
int main(int argc, char** argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> t;
  arr = vector<string>(n);
  for (int i = 0; i < n; ++i) cin >> arr[i];
  memset(when, 0, sizeof(when));
  queue<pair<int, int>> q;
  unordered_set<int> visited;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      for (int a = 0; a < 4; ++a) {
        int x = dirs[a][0] + i, y = dirs[a][1] + j;
        if (x < 0 || y < 0 || x >= n || y >= m) continue;
        if (arr[x][y] == arr[i][j]) {
          q.emplace(i, j);
          visited.insert(i * m + j);
          break;
        }
      }
    }
  }
  int level = 1;
  while (!q.empty()) {
    queue<pair<int, int>> nq;
    while (!q.empty()) {
      auto [x, y] = q.front();
      q.pop();
      when[x][y] = level;
      for (int i = 0; i < 4; ++i) {
        int xx = dirs[i][0] + x, yy = dirs[i][1] + y;
        if (xx < 0 || yy < 0 || xx >= n || yy >= m) continue;
        if (visited.find(xx * m + yy) != visited.end() ||
            arr[xx][yy] == arr[x][y])
          continue;
        visited.insert(xx * m + yy);
        nq.emplace(xx, yy);
      }
    }
    swap(q, nq);
    ++level;
  }
  for (int i = 0; i < t; ++i) {
    long long p;
    int x, y;
    cin >> x >> y >> p;
    x--, y--;
    if (when[x][y] == 0)
      cout << arr[x][y] << endl;
    else {
      long long ret = p - when[x][y];
      if (ret < 0)
        cout << arr[x][y] << endl;
      else if (ret & 1) {
        cout << arr[x][y] << endl;
      } else {
        cout << ((arr[x][y] - '0' + 1) & 1) << endl;
      }
    }
  }
  return 0;
}
