#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int inf = 1e9;
struct node {
  int col, st, x, y;
} g[maxn][maxn];
int cnt = 0;
queue<node> q;
int n, m, t;
bool check(int x, int y) {
  if (x >= 1 && x <= n && y >= 1 && y <= m)
    return true;
  else
    return false;
}
int main() {
  cin >> n >> m >> t;
  char c;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> c;
      if (c == '1')
        g[i][j].col = 1;
      else
        g[i][j].col = 0;
      g[i][j].st = inf, g[i][j].x = i, g[i][j].y = j;
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      bool flag = false;
      int x, y;
      x = i + 1, y = j;
      if (check(x, y) && g[x][y].col == g[i][j].col) flag = true;
      x = i - 1, y = j;
      if (check(x, y) && g[x][y].col == g[i][j].col) flag = true;
      x = i, y = j + 1;
      if (check(x, y) && g[x][y].col == g[i][j].col) flag = true;
      x = i, y = j - 1;
      if (check(x, y) && g[x][y].col == g[i][j].col) flag = true;
      if (flag) {
        g[i][j].st = 1;
        cnt++;
      }
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      bool flag = false;
      int x, y;
      x = i + 1, y = j;
      if (check(x, y) && g[x][y].st == inf && g[i][j].st != inf) flag = true;
      x = i - 1, y = j;
      if (check(x, y) && g[x][y].st == inf && g[i][j].st != inf) flag = true;
      x = i, y = j + 1;
      if (check(x, y) && g[x][y].st == inf && g[i][j].st != inf) flag = true;
      x = i, y = j - 1;
      if (check(x, y) && g[x][y].st == inf && g[i][j].st != inf) flag = true;
      if (flag) q.push(g[i][j]);
    }
  while (!q.empty()) {
    node now = q.front();
    q.pop();
    int x, y, i = now.x, j = now.y;
    x = i + 1, y = j;
    if (check(x, y)) {
      if (g[x][y].st == inf) {
        g[x][y].st = min(g[x][y].st, g[i][j].st + 1);
        q.push(g[x][y]);
      } else
        g[x][y].st = min(g[x][y].st, g[i][j].st + 1);
    }
    x = i - 1, y = j;
    if (check(x, y)) {
      if (g[x][y].st == inf) {
        g[x][y].st = min(g[x][y].st, g[i][j].st + 1);
        q.push(g[x][y]);
      } else
        g[x][y].st = min(g[x][y].st, g[i][j].st + 1);
    }
    x = i, y = j + 1;
    if (check(x, y)) {
      if (g[x][y].st == inf) {
        g[x][y].st = min(g[x][y].st, g[i][j].st + 1);
        q.push(g[x][y]);
      } else
        g[x][y].st = min(g[x][y].st, g[i][j].st + 1);
    }
    x = i, y = j - 1;
    if (check(x, y)) {
      if (g[x][y].st == inf) {
        g[x][y].st = min(g[x][y].st, g[i][j].st + 1);
        q.push(g[x][y]);
      } else
        g[x][y].st = min(g[x][y].st, g[i][j].st + 1);
    }
  }
  while (t--) {
    int x, y;
    long long time;
    cin >> x >> y >> time;
    if (g[x][y].st == inf) {
      cout << g[x][y].col << "\n";
      continue;
    }
    time -= (long long)(g[x][y].st - 1);
    int ans;
    if (time % 2 == 1) {
      ans = (g[x][y].col + 1) % 2;
    } else
      ans = g[x][y].col;
    cout << ans << "\n";
  }
}
