const int INF = (1 << 30);
const int NMAX = 1005;
int N, M, T;
char s[NMAX][NMAX];
bool bad[NMAX][NMAX];
bool seen[NMAX][NMAX];
int dist[NMAX][NMAX];
char color[NMAX][NMAX];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
bool Inside(int i, int j)
{
return 1 <= i && i <= N && 1 <= j && j <= M;
}
int Fill(int startx, int starty, char val)
{
int cnt = 1;
stack <pii> st;
st.push(mp(startx, starty));
seen[startx][starty] = true;
while (!st.empty())
{
int i = st.top().first;
int j = st.top().second;
st.pop();
for (int k = 0; k < 4; ++k)
{
int x = i + dx[k];
int y = j + dy[k];
if (Inside(x, y) && seen[x][y] == false && s[x][y] == val)
{
st.push(mp(x, y));
seen[x][y] = true;
++cnt;
}
}
}
return cnt;
}
int main()
{
#ifndef ONLINE_JUDGE
ifstream cin("test.in");
#endif
#ifdef ONLINE_JUDGE
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#endif
cin >> N >> M >> T;
for (int i = 1; i <= N; ++i)
cin >> (s[i] + 1);
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= M; ++j)
{
if (seen[i][j] == false)
if (Fill(i, j, s[i][j]) == 1)
bad[i][j] = true;
}
queue <pair <pii ,char>> q;
for (int i = 1;i <= N;++i)
for (int j = 1; j <= M; ++j)
{
color[i][j] = s[i][j];
if (bad[i][j])
dist[i][j] = INF;
else
{
dist[i][j] = 0;
q.push(mp(mp(i, j), s[i][j]));
}
}
while (!q.empty())
{
int i = q.front().first.first;
int j = q.front().first.second;
char from = q.front().second;
q.pop();
for (int k = 0; k < 4; ++k)
{
int x = i + dx[k];
int y = j + dy[k];
if (Inside(x, y) && dist[x][y] > dist[i][j] + 1)
{
color[x][y] = from;
dist[x][y] = dist[i][j] + 1;
q.push(mp(mp(x, y), from));
}
}
}
while (T-- > 0)
{
long long i, j, p;
cin >> i >> j >> p;
if (dist[i][j] == INF || p <= dist[i][j])
cout << (s[i][j] - '0') << "\n";
else
{
p -= dist[i][j];
if (p % 2)
cout << ('1' - s[i][j]) << "\n";
else
cout << (s[i][j] - '0') << "\n";
}
}
#ifndef ONLINE_JUDGE
cout << "\n\n\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
cin.close();
#endif
return 0;
}