#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long inf = 2e18;
mt19937 ran(time(0));
struct st {
  int col, d, x, y;
  friend bool operator<(const st &st1, const st &st2) {
    return tie(st1.d, st1.col, st1.x, st1.y) <
           tie(st2.d, st2.col, st2.x, st2.y);
  }
};
vector<pair<int, int>> wasd = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vector<vector<int>> g;
bool any(int x, int y) {
  for (int i = 0; i < 4; i++) {
    if (g[x][y] == g[x + wasd[i].first][y + wasd[i].second]) return true;
  }
  return false;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int n, m, t;
  cin >> n >> m >> t;
  char ch;
  g.resize(n + 2, vector<int>(m + 2, -1));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> ch;
      if (ch == '1')
        g[i][j] = 1;
      else
        g[i][j] = 0;
    }
  }
  vector<vector<long long>> dist(n + 2, vector<long long>(m + 2, inf));
  set<st> q;
  vector<vector<bool>> vis(n + 2, vector<bool>(m + 2, false));
  for (int i = 0; i <= n + 1; i++) {
    vis[i][0] = vis[i][m + 1] = true;
  }
  for (int i = 0; i <= m + 1; i++) {
    vis[0][i] = vis[n + 1][i] = true;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (!vis[i][j] && any(i, j)) {
        q.insert({g[i][j], 0, i, j});
        vis[i][j] = true;
      }
    }
  }
  pair<int, int> cur;
  int curc, curd;
  int tx, ty;
  while (!q.empty()) {
    cur = {q.begin()->x, q.begin()->y};
    curc = q.begin()->col;
    curd = q.begin()->d;
    dist[cur.first][cur.second] = curd;
    for (int i = 0; i < 4; i++) {
      tx = cur.first + wasd[i].first;
      ty = cur.second + wasd[i].second;
      if (!vis[tx][ty]) {
        if (g[tx][ty] != curc) {
          q.insert({g[tx][ty], curd + 1, tx, ty});
        } else {
          q.insert({g[tx][ty], curd, tx, ty});
        }
        vis[tx][ty] = true;
      }
    }
    q.erase(q.begin());
  }
  int l, r;
  long long p;
  for (int i = 0; i < t; i++) {
    cin >> l >> r >> p;
    if (p <= dist[l][r]) {
      cout << g[l][r] << "\n";
    } else {
      if ((p - dist[l][r]) % 2 == 0) {
        cout << g[l][r] << "\n";
      } else {
        cout << 1 - g[l][r] << "\n";
      }
    }
  }
  return 0;
}
