#include <bits/stdc++.h>
void err() { std::cout << std::endl; }
template <typename T, typename... Args>
void err(T a, Args... args) {
  std::cout << a << ' ';
  err(args...);
}
template <template <typename...> class T, typename t, typename... A>
void err(const T<t> &arg, const A &...args) {
  for (auto &v : arg) std::cout << v << ' ';
  err(args...);
}
using namespace std;
const int N = 1e3 + 5;
const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
int n, m, t;
char s[N][N];
int a[N][N], dis[N][N];
bool vis[N][N];
void run() {
  cin >> n >> m >> t;
  for (int i = 1; i <= n; i++) {
    cin >> (s[i] + 1);
    for (int j = 1; j <= m; j++) {
      a[i][j] = (s[i][j] - '0');
    }
  }
  auto ok = [&](int nx, int ny) {
    return nx >= 1 && nx <= n && ny >= 1 && ny <= m;
  };
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (!vis[i][j]) {
        for (int k = 0; k < 4; k++) {
          int x = i + dx[k], y = j + dy[k];
          if (ok(x, y) && a[x][y] == a[i][j]) {
            vis[i][j] = true;
          }
        }
      }
    }
  }
  memset(dis, 0x3f3f3f3f, sizeof(dis));
  queue<pair<int, int> > q;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (vis[i][j]) {
        q.push(make_pair(i, j));
        dis[i][j] = 0;
      }
    }
  }
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    q.pop();
    int x = cur.first, y = cur.second;
    auto ok = [&](int nx, int ny) {
      return nx >= 1 && nx <= n && ny >= 1 && ny <= m && !vis[nx][ny] &&
             dis[nx][ny] > dis[x][y] + 1;
    };
    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k], ny = y + dy[k];
      if (ok(nx, ny)) {
        dis[nx][ny] = dis[x][y] + 1;
        vis[nx][ny] = true;
        q.push(make_pair(nx, ny));
      }
    }
  }
  while (t--) {
    int i, j;
    long long p;
    cin >> i >> j >> p;
    if (p <= dis[i][j] || dis[i][j] == 0x3f3f3f3f) {
      cout << a[i][j] << '\n';
    } else {
      int r = (p - dis[i][j]) % 2;
      cout << (a[i][j] ^ r) << '\n';
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(20);
  run();
  return 0;
}
