#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, t;
  cin >> n >> m >> t;
  string s[n];
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
  }
  long long int time[n][m];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; j++) {
      time[i][j] = -1;
    }
  }
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i > 0 && s[i - 1][j] == s[i][j]) {
        time[i][j] = 0;
        q.push(make_pair(i, j));
      } else if (i < (n - 1) && s[i + 1][j] == s[i][j]) {
        time[i][j] = 0;
        q.push(make_pair(i, j));
      } else if (j > 0 && s[i][j - 1] == s[i][j]) {
        time[i][j] = 0;
        q.push(make_pair(i, j));
      } else if (j < (m - 1) && s[i][j + 1] == s[i][j]) {
        time[i][j] = 0;
        q.push(make_pair(i, j));
      }
    }
  }
  while (!q.empty()) {
    int x, y;
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if (x > 0 && time[x - 1][y] == -1) {
      time[x - 1][y] = time[x][y] + 1;
      q.push(make_pair(x - 1, y));
    }
    if (x < (n - 1) && time[x + 1][y] == -1) {
      time[x + 1][y] = time[x][y] + 1;
      q.push(make_pair(x + 1, y));
    }
    if (y > 0 && time[x][y - 1] == -1) {
      time[x][y - 1] = time[x][y] + 1;
      q.push(make_pair(x, y - 1));
    }
    if (y < (m - 1) && time[x][y + 1] == -1) {
      time[x][y + 1] = time[x][y] + 1;
      q.push(make_pair(x, y + 1));
    }
  }
  while (t--) {
    long long int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (time[a][b] == -1 || time[a][b] >= c) {
      cout << s[a][b] << "\n";
    } else {
      if ((c - time[a][b]) % 2ll == 0) {
        cout << s[a][b] << "\n";
      } else {
        cout << ((s[a][b] - '0') ^ 1) << "\n";
      }
    }
  }
}
