// Nguyen Anh Tu
#include <bits/stdc++.h>
#define FOR(x,a,b) for(int x=a;x<=b;x++)
#define FORD(x,a,b) for(int x=a;x>=b;x--)
#define maxn 1005
#define maxc 1000000007
#define MOD 1000000007
#define reset(x, y) memset(x,y,sizeof(x))
#define task ""
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define ll long long
#define bit(p,x) ((x>>p) & 1)
#define remain(a,b) (a+b >= MOD) ? (a+b - MOD) : (a+b)
#define piii pair<pii, int>
using namespace std;
int t, m, n,a[maxn][maxn];
vector<int> v[maxn];
queue<piii> q;
int pos[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int vis[maxn][maxn];
int f[maxn][maxn];
bool check(int x, int y){
    for(int i = 0; i<4; i++){
		int nx = x+pos[i][0], ny = y+pos[i][1];
		if(a[nx][ny]==a[x][y])
			return 1;
	}
	return 0;
}
void add(int x, int y, int val){
    for(int i = 0; i<4; i++){
		int nx = x+pos[i][0], ny = y+pos[i][1];
		if(!vis[nx][ny]){
		    vis[nx][ny] = 1;
            f[nx][ny] = val;
            q.push({{nx,ny},val});
		}
	}
}
void BFS(){
    reset(f,64);
    for(int i=1; i<=n; ++i)
        for(int j=1; j<=m; ++j)
            if(check(i,j)) {
                vis[i][j] = 1;
                f[i][j] = 0;
                q.push({{i,j},0});
        }
    while(q.size()){
        piii x = q.front();
        q.pop();
        int w = x.S;
        int u = x.F.F;
        int v = x.F.S;
        add(u,v, f[u][v] + 1);
    }
}
int main()
{
    ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL);
    if(fopen(task".inp", "r"))
    {
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    cin >> n >> m >> t;
    reset(a,2);
    for(int i=1; i<=n ;++i){
        string s;
        cin >> s;
        for(int j=1; j<=m; ++j) a[i][j] = s[j-1] - '0';
    }
    BFS();
    while(t--){
        int u,v,p;
        cin >> u >> v >> p;
        p -= f[u][v];
        if(p < 0){
            cout << a[u][v] << '\n';
            continue;
        }
        cout << (a[u][v] ^ (p & 1)) << '\n';
    }
    return 0;
}


// Nguyen Anh Tu
#include <bits/stdc++.h>
#define FOR(x,a,b) for(int x=a;x<=b;x++)
#define FORD(x,a,b) for(int x=a;x>=b;x--)
#define maxn 1005
#define maxc 1000000007
#define MOD 1000000007
#define reset(x, y) memset(x,y,sizeof(x))
#define task ""
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define ll long long
#define bit(p,x) ((x>>p) & 1)
#define remain(a,b) (a+b >= MOD) ? (a+b - MOD) : (a+b)
#define piii pair<pii, int>
using namespace std;
int t, m, n,a[maxn][maxn];
vector<int> v[maxn];
queue<piii> q;
int pos[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int vis[maxn][maxn];
int f[maxn][maxn];
bool check(int x, int y){
    for(int i = 0; i<4; i++){
		int nx = x+pos[i][0], ny = y+pos[i][1];
		if(a[nx][ny]==a[x][y])
			return 1;
	}
	return 0;
}
void add(int x, int y, int val){
    for(int i = 0; i<4; i++){
		int nx = x+pos[i][0], ny = y+pos[i][1];
		if(!vis[nx][ny]){
		    vis[nx][ny] = 1;
            f[nx][ny] = val;
            q.push({{nx,ny},val});
		}
	}
}
void BFS(){
    reset(f,64);
    for(int i=1; i<=n; ++i)
        for(int j=1; j<=m; ++j)
            if(check(i,j)) {
                vis[i][j] = 1;
                f[i][j] = 0;
                q.push({{i,j},0});
        }
    while(q.size()){
        piii x = q.front();
        q.pop();
        int w = x.S;
        int u = x.F.F;
        int v = x.F.S;
        add(u,v, f[u][v] + 1);
    }
}
int main()
{
    ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL);
    if(fopen(task".inp", "r"))
    {
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    cin >> n >> m >> t;
    reset(a,2);
    reset(vis,1);
    for(int i=1; i<=n ;++i){
        string s;
        cin >> s;
        for(int j=1; j<=m; ++j) a[i][j] = s[j-1] - '0',vis[i][j] = 0;
    }
    BFS();
    while(t--){
        int u,v;
        ll p;
        cin >> u >> v >> p;
        p -= f[u][v];
        if(p < 0){
            cout << a[u][v] << '\n';
            continue;
        }
        cout << (a[u][v] ^ (p & 1)) << '\n';
    }
    return 0;
}


// Nguyen Anh Tu
#include <bits/stdc++.h>
#define FOR(x,a,b) for(int x=a;x<=b;x++)
#define FORD(x,a,b) for(int x=a;x>=b;x--)
#define maxn 1005
#define maxc 1000000007
#define MOD 1000000007
#define reset(x, y) memset(x,y,sizeof(x))
#define task ""
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define ll long long
#define bit(p,x) ((x>>p) & 1)
#define remain(a,b) (a+b >= MOD) ? (a+b - MOD) : (a+b)
#define piii pair<pii, int>
using namespace std;
int t, m, n,a[maxn][maxn];
vector<int> v[maxn];
queue<piii> q;
int pos[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int vis[maxn][maxn];
int f[maxn][maxn];
bool check(int x, int y){
    for(int i = 0; i<4; i++){
		int nx = x+pos[i][0], ny = y+pos[i][1];
		if(a[nx][ny]==a[x][y])
			return 1;
	}
	return 0;
}
void add(int x, int y, int val){
    for(int i = 0; i<4; i++){
		int nx = x+pos[i][0], ny = y+pos[i][1];
		if(!vis[nx][ny]){
		    vis[nx][ny] = 1;
            f[nx][ny] = val;
            q.push({{nx,ny},val});
		}
	}
}
void BFS(){
    reset(f,64);
    for(int i=1; i<=n; ++i)
        for(int j=1; j<=m; ++j)
            if(check(i,j)) {
                vis[i][j] = 1;
                f[i][j] = 0;
                q.push({{i,j},0});
        }
    while(q.size()){
        piii x = q.front();
        q.pop();
        int w = x.S;
        int u = x.F.F;
        int v = x.F.S;
        add(u,v, f[u][v] + 1);
    }
}
int main()
{
    ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL);
    if(fopen(task".inp", "r"))
    {
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    cin >> n >> m >> t;
    reset(a,2);
    reset(vis,1);
    for(int i=1; i<=n ;++i){
        string s;
        cin >> s;
        for(int j=1; j<=m; ++j) a[i][j] = s[j-1] - '0',vis[i][j] = 0;
    }
    BFS();
    while(t--){
        int u,v,p;
        cin >> u >> v >> p;
        p -= f[u][v];
        if(p < 0){
            cout << a[u][v] << '\n';
            continue;
        }
        cout << (a[u][v] ^ (p & 1)) << '\n';
    }
    return 0;
}


// Nguyen Anh Tu
#include <bits/stdc++.h>
#define FOR(x,a,b) for(int x=a;x<=b;x++)
#define FORD(x,a,b) for(int x=a;x>=b;x--)
#define maxn 1005
#define maxc 1000000007
#define MOD 1000000007
#define reset(x, y) memset(x,y,sizeof(x))
#define task ""
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define ll long long
#define bit(p,x) ((x>>p) & 1)
#define remain(a,b) (a+b >= MOD) ? (a+b - MOD) : (a+b)
#define piii pair<pii, int>
using namespace std;
int t, m, n,a[maxn][maxn];
vector<int> v[maxn];
queue<piii> q;
int pos[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int vis[maxn][maxn];
ll f[maxn][maxn];
bool check(int x, int y){
    for(int i = 0; i<4; i++){
		int nx = x+pos[i][0], ny = y+pos[i][1];
		if(a[nx][ny]==a[x][y])
			return 1;
	}
	return 0;
}
void add(int x, int y, int val){
    for(int i = 0; i<4; i++){
		int nx = x+pos[i][0], ny = y+pos[i][1];
		if(!vis[nx][ny]){
		    vis[nx][ny] = 1;
            f[nx][ny] = val;
            q.push({{nx,ny},val});
		}
	}
}
void BFS(){
    for(int i=1; i<=n; ++i)
        for(int j=1; j<=m; ++j)
            if(check(i,j)) {
                vis[i][j] = 1;
                f[i][j] = 0;
                q.push({{i,j},0});
        }
    while(q.size()){
        piii x = q.front();
        q.pop();
        int w = x.S;
        int u = x.F.F;
        int v = x.F.S;
        add(u,v, f[u][v] + 1);
    }
}
int main()
{
    ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL);
    if(fopen(task".inp", "r"))
    {
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    cin >> n >> m >> t;
    reset(a,2);
    reset(vis,1);
    for(int i=1; i<=n ;++i){
        string s;
        cin >> s;
        for(int j=1; j<=m; ++j) a[i][j] = s[j-1] - '0',vis[i][j] = 0, f[i][j] = 2e18;
    }
    BFS();
    while(t--){
        int u,v;
        ll p;
        cin >> u >> v >> p;
        p -= f[u][v];
        if(p < 0){
            cout << a[u][v] << '\n';
            continue;
        }
        cout << (a[u][v] ^ (p & 1)) << '\n';
    }
    return 0;
}


// Nguyen Anh Tu
#include <bits/stdc++.h>
#define FOR(x,a,b) for(int x=a;x<=b;x++)
#define FORD(x,a,b) for(int x=a;x>=b;x--)
#define maxn 1005
#define maxc 1000000007
#define MOD 1000000007
#define reset(x, y) memset(x,y,sizeof(x))
#define task ""
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define ll long long
#define bit(p,x) ((x>>p) & 1)
#define remain(a,b) (a+b >= MOD) ? (a+b - MOD) : (a+b)
#define piii pair<pii, int>
using namespace std;
int t, m, n,a[maxn][maxn];
vector<int> v[maxn];
queue<piii> q;
int pos[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int vis[maxn][maxn];
int f[maxn][maxn];
bool check(int x, int y){
    for(int i = 0; i<4; i++){
		int nx = x+pos[i][0], ny = y+pos[i][1];
		if(a[nx][ny]==a[x][y])
			return 1;
	}
	return 0;
}
void add(int x, int y, int val){
    for(int i = 0; i<4; i++){
		int nx = x+pos[i][0], ny = y+pos[i][1];
		if(!vis[nx][ny]){
		    vis[nx][ny] = 1;
            f[nx][ny] = val;
            q.push({{nx,ny},val});
		}
	}
}
void BFS(){
    for(int i=1; i<=n; ++i)
        for(int j=1; j<=m; ++j)
            if(check(i,j)) {
                vis[i][j] = 1;
                f[i][j] = 0;
                q.push({{i,j},0});
        }
    while(q.size()){
        piii x = q.front();
        q.pop();
        int w = x.S;
        int u = x.F.F;
        int v = x.F.S;
        add(u,v, f[u][v] + 1);
    }
}
int main()
{
    ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL);
    if(fopen(task".inp", "r"))
    {
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    cin >> n >> m >> t;
    reset(a,2);
    reset(vis,1);
    for(int i=1; i<=n ;++i){
        string s;
        cin >> s;
        for(int j=1; j<=m; ++j) a[i][j] = s[j-1] - '0',vis[i][j] = 0, f[i][j] = 2e18;
    }
    BFS();
    while(t--){
        int u,v;
        ll p;
        cin >> u >> v >> p;
        p -= f[u][v];
        if(p < 0){
            cout << a[u][v] << '\n';
            continue;
        }
        cout << (a[u][v] ^ (p & 1)) << '\n';
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <random>
#include <set>
#include <queue>

using namespace std;

// mt19937 rng(5);
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define fo(i, n) for (int i = 0; i < n; ++i)

#define N 11

vector<pair<int, int>> dirs = {
    {1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int main()
{

    int a[N][N];
    int res[N][N];

    ios::sync_with_stdio(false);
    cin.tie(0);

    int r, c;
    cin >> r >> c;
    int t;
    cin >> t;

    for (int i = 0; i <= r + 1; i++)
    {
        a[i][0] = a[i][c + 1] = 2;
    }
    for (int j = 0; j <= c + 1; j++)
    {
        a[0][j] = a[r + 1][j] = 2;
    }

    for (int i = 1; i <= r; i++)
    {
        string s;
        cin >> s;
        for (int j = 1; j <= c; j++)
        {
            a[i][j] = s[j - 1] - '0';
            res[i][j] = -1;
            // cout << a[i][j];
        }
    }

    // for (int i = 0; i <= r + 1; i++)
    // {
    //     for (int j = 0; j <= c + 1; j++)
    //     {
    //         cout << a[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    // for (int i = 1; i <= r; i++)
    // {
    //     for (int j = 1; j <= c; j++)
    //     {
    //         cout << res[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    for (int i = 1; i <= r; i++)
    {
        for (int j = 1; j <= c; j++)
        {
            bool hasSameColorNeighs = false;

            for (auto dir : dirs)
            {
                int ii = i + dir.first;
                int jj = j +
                         dir.second;

                if (a[i][j] == a[ii][jj])
                {
                    hasSameColorNeighs = true;
                    break;
                }
            }

            if (hasSameColorNeighs)
            {
                res[i][j] = 0;
            }
        }
    }

    // for (int i = 1; i <= r; i++)
    // {
    //     for (int j = 1; j <= c; j++)
    //     {
    //         cout << res[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    for (int i = 1; i <= r; i++)
    {
        for (int j = 1; j <= c; j++)
        {
            if (res[i][j] >= 0)
            {

                continue;
            }

            map<pair<int, int>, int> was;
            queue<pair<int, int>> q;
            q.push({i, j});
            was[{i, j}] = true;
            while (!q.empty())
            {
                auto p = q.front();
                q.pop();

                int ii = p.first;
                int jj = p.second;
                if (res[ii][jj] == 0)
                {
                    res[i][j] = abs(i - ii) + abs(j - jj);
                    break;
                }

                for (auto dir : dirs)
                {
                    int iii = ii + dir.first;
                    int jjj = jj +
                              dir.second;

                    if (!was[{iii, jjj}] && iii >= 1 && jjj >= 1 && iii <= r && jjj <= c)
                    {
                        was[{iii, jjj}] = true;
                        q.push({iii, jjj});
                    }
                }
            }
        }
    }

    fo(tt, t)
    {
        int i, j, p;
        cin >> i >> j >> p;
        // i--;j--;

        if (res[i][j] < 0 || res[i][j] >= p)
        {
            cout << a[i][j] << endl;
        }
        else
        {
            int v = a[i][j] + p - res[i][j];
            cout << (v % 2) << endl;
            // cout << (res[i][j]) << endl;
        }
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <random>
#include <set>
#include <queue>

using namespace std;

// mt19937 rng(5);
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define fo(i, n) for (int i = 0; i < n; ++i)

#define N 1012

vector<pair<int, int>> dirs = {
    {1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int main()
{
    int a[N][N];
    int res[N][N];

    ios::sync_with_stdio(false);
    cin.tie(0);

    int r, c;
    cin >> r >> c;
    int t;
    cin >> t;

    for (int i = 0; i <= r + 1; i++)
    {
        a[i][0] = a[i][c + 1] = 2;
    }
    for (int j = 0; j <= c + 1; j++)
    {
        a[0][j] = a[r + 1][j] = 2;
    }

    for (int i = 1; i <= r; i++)
    {
        string s;
        cin >> s;
        for (int j = 1; j <= c; j++)
        {
            a[i][j] = s[j - 1] - '0';
            res[i][j] = -1;
            // cout << a[i][j];
        }
    }

    // for (int i = 0; i <= r + 1; i++)
    // {
    //     for (int j = 0; j <= c + 1; j++)
    //     {
    //         cout << a[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    // for (int i = 1; i <= r; i++)
    // {
    //     for (int j = 1; j <= c; j++)
    //     {
    //         cout << res[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    for (int i = 1; i <= r; i++)
    {
        for (int j = 1; j <= c; j++)
        {
            bool hasSameColorNeighs = false;

            for (auto dir : dirs)
            {
                int ii = i + dir.first;
                int jj = j +
                         dir.second;

                if (a[i][j] == a[ii][jj])
                {
                    hasSameColorNeighs = true;
                    break;
                }
            }

            if (hasSameColorNeighs)
            {
                res[i][j] = 0;
            }
        }
    }

    // for (int i = 1; i <= r; i++)
    // {
    //     for (int j = 1; j <= c; j++)
    //     {
    //         cout << res[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    for (int i = 1; i <= r; i++)
    {
        for (int j = 1; j <= c; j++)
        {
            if (res[i][j] >= 0)
            {

                continue;
            }

            map<pair<int, int>, int> was;
            queue<pair<int, int>> q;
            q.push({i, j});
            was[{i, j}] = true;
            while (!q.empty())
            {
                auto p = q.front();
                q.pop();

                int ii = p.first;
                int jj = p.second;
                if (res[ii][jj] == 0)
                {
                    res[i][j] = abs(i - ii) + abs(j - jj);
                    break;
                }

                for (auto dir : dirs)
                {
                    int iii = ii + dir.first;
                    int jjj = jj +
                              dir.second;

                    if (!was[{iii, jjj}] && iii >= 1 && jjj >= 1 && iii <= r && jjj <= c)
                    {
                        was[{iii, jjj}] = true;
                        q.push({iii, jjj});
                    }
                }
            }
        }
    }

    fo(tt, t)
    {
        int i, j, p;
        cin >> i >> j >> p;
        // i--;j--;

        if (res[i][j] < 0 || res[i][j] >= p)
        {
            cout << a[i][j] << endl;
        }
        else
        {
            int v = a[i][j] + p - res[i][j];
            cout << (v % 2) << endl;
            // cout << (res[i][j]) << endl;
        }
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <random>
#include <set>
#include <queue>

using namespace std;

// mt19937 rng(5);
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define fo(i, n) for (int i = 0; i < n; ++i)

#define N 1012

vector<pair<int, int>> dirs = {
    {1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int main()
{
    int a[N][N];
    int res[N][N];

    ios::sync_with_stdio(false);
    cin.tie(0);

    int r, c;
    cin >> r >> c;
    int t;
    cin >> t;

    for (int i = 0; i <= r + 1; i++)
    {
        a[i][0] = a[i][c + 1] = 2;
    }
    for (int j = 0; j <= c + 1; j++)
    {
        a[0][j] = a[r + 1][j] = 2;
    }

    for (int i = 1; i <= r; i++)
    {
        string s;
        cin >> s;
        for (int j = 1; j <= c; j++)
        {
            a[i][j] = s[j - 1] - '0';
            res[i][j] = -1;
            // cout << a[i][j];
        }
    }

    // for (int i = 0; i <= r + 1; i++)
    // {
    //     for (int j = 0; j <= c + 1; j++)
    //     {
    //         cout << a[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    // for (int i = 1; i <= r; i++)
    // {
    //     for (int j = 1; j <= c; j++)
    //     {
    //         cout << res[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    for (int i = 1; i <= r; i++)
    {
        for (int j = 1; j <= c; j++)
        {
            bool hasSameColorNeighs = false;

            for (auto dir : dirs)
            {
                int ii = i + dir.first;
                int jj = j +
                         dir.second;

                if (a[i][j] == a[ii][jj])
                {
                    hasSameColorNeighs = true;
                    break;
                }
            }

            if (hasSameColorNeighs)
            {
                res[i][j] = 0;
            }
        }
    }

    // for (int i = 1; i <= r; i++)
    // {
    //     for (int j = 1; j <= c; j++)
    //     {
    //         cout << res[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    for (int i = 1; i <= r; i++)
    {
        for (int j = 1; j <= c; j++)
        {
            if (res[i][j] >= 0)
            {

                continue;
            }

            map<pair<int, int>, int> was;
            queue<pair<int, int>> q;
            q.push({i, j});
            was[{i, j}] = true;
            while (!q.empty())
            {
                auto p = q.front();
                q.pop();

                int ii = p.first;
                int jj = p.second;
                if (res[ii][jj] == 0)
                {
                    res[i][j] = abs(i - ii) + abs(j - jj);
                    break;
                }

                for (auto dir : dirs)
                {
                    int iii = ii + dir.first;
                    int jjj = jj +
                              dir.second;

                    if (!was[{iii, jjj}] && iii >= 1 && jjj >= 1 && iii <= r && jjj <= c)
                    {
                        was[{iii, jjj}] = true;
                        q.push({iii, jjj});
                    }
                }
            }
        }
    }

    fo(tt, t)
    {
        int i, j;
        long long p;
        cin >> i >> j >> p;
        // i--;j--;

        long long r = res[i][j];

        if (r < 0 || r >= p)
        {
            cout << a[i][j] << endl;
        }
        else
        {
            p -= r;
            p += a[i][j];
            cout << (p % 2) << endl;
            // cout << (res[i][j]) << endl;
        }
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <random>
#include <set>
#include <queue>

using namespace std;

// mt19937 rng(5);
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define fo(i, n) for (int i = 0; i < n; ++i)

#define N 1012

vector<pair<int, int>> dirs = {
    {1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int main()
{
    int a[N][N];
    int res[N][N];

    ios::sync_with_stdio(false);
    cin.tie(0);

    int r, c;
    cin >> r >> c;
    int t;
    cin >> t;

    for (int i = 0; i <= r + 1; i++)
    {
        a[i][0] = a[i][c + 1] = 2;
    }
    for (int j = 0; j <= c + 1; j++)
    {
        a[0][j] = a[r + 1][j] = 2;
    }

    for (int i = 1; i <= r; i++)
    {
        string s;
        cin >> s;
        for (int j = 1; j <= c; j++)
        {
            a[i][j] = s[j - 1] - '0';
            res[i][j] = -1;
            // cout << a[i][j];
        }
    }

    // for (int i = 0; i <= r + 1; i++)
    // {
    //     for (int j = 0; j <= c + 1; j++)
    //     {
    //         cout << a[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    // for (int i = 1; i <= r; i++)
    // {
    //     for (int j = 1; j <= c; j++)
    //     {
    //         cout << res[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    for (int i = 1; i <= r; i++)
    {
        for (int j = 1; j <= c; j++)
        {
            bool hasSameColorNeighs = false;

            for (auto dir : dirs)
            {
                int ii = i + dir.first;
                int jj = j +
                         dir.second;

                if (a[i][j] == a[ii][jj])
                {
                    hasSameColorNeighs = true;
                    break;
                }
            }

            if (hasSameColorNeighs)
            {
                res[i][j] = 0;
            }
        }
    }

    for (int i = 1; i <= r; i++)
    {
        for (int j = 1; j <= c; j++)
        {
            queue<pair<int, int>> q;
            if (res[i][j] >= 0)
            {
                q.push({i, j});
            }

            while (!q.empty())
            {
                auto p = q.front();
                q.pop();
                for (auto dir : dirs)
                {
                    int ii = dir.first + p.first;
                    int jj = dir.second + p.second;

                    if (ii < 1 || jj < 1 || ii > r || jj > c)
                    {
                        continue;
                    }

                    if (res[ii][jj] < 0)
                    {
                        res[ii][jj] = abs(dir.first) + abs(dir.second) + res[p.first][p.second];
                        q.push({ii, jj});
                    }
                }
            }
        }
    }

    // for (int i = 1; i <= r; i++)
    // {
    //     for (int j = 1; j <= c; j++)
    //     {
    //         if (res[i][j] >= 0)
    //         {
    //             continue;
    //         }

    //         map<pair<int, int>, int> was;
    //         queue<pair<int, int>> q;
    //         q.push({i, j});
    //         was[{i, j}] = true;
    //         while (!q.empty())
    //         {
    //             auto p = q.front();
    //             q.pop();

    //             int ii = p.first;
    //             int jj = p.second;
    //             auto v = res[ii][jj];
    //             if (v == 0)
    //             {
    //                 res[i][j] = abs(i - ii) + abs(j - jj);
    //                 break;
    //             }

    //             for (auto dir : dirs)
    //             {
    //                 int iii = ii + dir.first;
    //                 int jjj = jj +
    //                           dir.second;

    //                 if (!was[{iii, jjj}] && iii >= 1 && jjj >= 1 && iii <= r && jjj <= c)
    //                 {
    //                     was[{iii, jjj}] = true;
    //                     q.push({iii, jjj});
    //                 }
    //             }
    //         }
    //     }
    // }

    fo(tt, t)
    {
        int i, j;
        long long p;
        cin >> i >> j >> p;

        long long r = res[i][j];

        if (r < 0 || r >= p)
        {
            cout << a[i][j] << endl;
        }
        else
        {
            p -= r;
            p += a[i][j];
            cout << (p % 2) << endl;
            // cout << (res[i][j]) << endl;
        }
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <random>
#include <set>
#include <queue>

using namespace std;

// mt19937 rng(5);
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define fo(i, n) for (int i = 0; i < n; ++i)

#define N 1012

vector<pair<int, int>> dirs = {
    {1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int main()
{
    int a[N][N];
    int res[N][N];

    ios::sync_with_stdio(false);
    cin.tie(0);

    int r, c;
    cin >> r >> c;
    int t;
    cin >> t;

    for (int i = 0; i <= r + 1; i++)
    {
        a[i][0] = a[i][c + 1] = 2;
    }
    for (int j = 0; j <= c + 1; j++)
    {
        a[0][j] = a[r + 1][j] = 2;
    }

    for (int i = 1; i <= r; i++)
    {
        string s;
        cin >> s;
        for (int j = 1; j <= c; j++)
        {
            a[i][j] = s[j - 1] - '0';
            res[i][j] = -1;
            // cout << a[i][j];
        }
    }

    // for (int i = 0; i <= r + 1; i++)
    // {
    //     for (int j = 0; j <= c + 1; j++)
    //     {
    //         cout << a[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    // for (int i = 1; i <= r; i++)
    // {
    //     for (int j = 1; j <= c; j++)
    //     {
    //         cout << res[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    for (int i = 1; i <= r; i++)
    {
        for (int j = 1; j <= c; j++)
        {
            bool hasSameColorNeighs = false;

            for (auto dir : dirs)
            {
                int ii = i + dir.first;
                int jj = j +
                         dir.second;

                if (a[i][j] == a[ii][jj])
                {
                    hasSameColorNeighs = true;
                    break;
                }
            }

            if (hasSameColorNeighs)
            {
                res[i][j] = 0;
            }
        }
    }

    queue<pair<int, int>> q;
    for (int i = 1; i <= r; i++)
    {
        for (int j = 1; j <= c; j++)
        {
            if (res[i][j] >= 0)
            {
                q.push({i, j});
            }
        }
    }
    while (!q.empty())
    {
        auto p = q.front();
        q.pop();
        for (auto dir : dirs)
        {
            int ii = dir.first + p.first;
            int jj = dir.second + p.second;

            if (ii < 1 || jj < 1 || ii > r || jj > c)
            {
                continue;
            }

            if (res[ii][jj] < 0)
            {
                res[ii][jj] = abs(dir.first) + abs(dir.second) + res[p.first][p.second];
                q.push({ii, jj});
            }
        }
    }

    // for (int i = 1; i <= r; i++)
    // {
    //     for (int j = 1; j <= c; j++)
    //     {
    //         if (res[i][j] >= 0)
    //         {
    //             continue;
    //         }

    //         map<pair<int, int>, int> was;
    //         queue<pair<int, int>> q;
    //         q.push({i, j});
    //         was[{i, j}] = true;
    //         while (!q.empty())
    //         {
    //             auto p = q.front();
    //             q.pop();

    //             int ii = p.first;
    //             int jj = p.second;
    //             auto v = res[ii][jj];
    //             if (v == 0)
    //             {
    //                 res[i][j] = abs(i - ii) + abs(j - jj);
    //                 break;
    //             }

    //             for (auto dir : dirs)
    //             {
    //                 int iii = ii + dir.first;
    //                 int jjj = jj +
    //                           dir.second;

    //                 if (!was[{iii, jjj}] && iii >= 1 && jjj >= 1 && iii <= r && jjj <= c)
    //                 {
    //                     was[{iii, jjj}] = true;
    //                     q.push({iii, jjj});
    //                 }
    //             }
    //         }
    //     }
    // }

    fo(tt, t)
    {
        int i, j;
        long long p;
        cin >> i >> j >> p;

        long long r = res[i][j];

        if (r < 0 || r >= p)
        {
            cout << a[i][j] << endl;
        }
        else
        {
            p -= r;
            p += a[i][j];
            cout << (p % 2) << endl;
            // cout << (res[i][j]) << endl;
        }
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <random>
#include <set>
#include <queue>

using namespace std;

// mt19937 rng(5);
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define fo(i, n) for (int i = 0; i < n; ++i)

#define N 1012

vector<pair<int, int>> dirs = {
    {1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int main()
{
    int a[N][N];
    int res[N][N];

    ios::sync_with_stdio(false);
    cin.tie(0);

    int r, c;
    cin >> r >> c;
    int t;
    cin >> t;

    for (int i = 0; i <= r + 1; i++)
    {
        a[i][0] = a[i][c + 1] = 2;
    }
    for (int j = 0; j <= c + 1; j++)
    {
        a[0][j] = a[r + 1][j] = 2;
    }

    for (int i = 1; i <= r; i++)
    {
        string s;
        cin >> s;
        for (int j = 1; j <= c; j++)
        {
            a[i][j] = s[j - 1] - '0';
            res[i][j] = -1;
            // cout << a[i][j];
        }
    }

    // for (int i = 0; i <= r + 1; i++)
    // {
    //     for (int j = 0; j <= c + 1; j++)
    //     {
    //         cout << a[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    // for (int i = 1; i <= r; i++)
    // {
    //     for (int j = 1; j <= c; j++)
    //     {
    //         cout << res[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    for (int i = 1; i <= r; i++)
    {
        for (int j = 1; j <= c; j++)
        {
            bool hasSameColorNeighs = false;

            for (auto dir : dirs)
            {
                int ii = i + dir.first;
                int jj = j +
                         dir.second;

                if (a[i][j] == a[ii][jj])
                {
                    hasSameColorNeighs = true;
                    break;
                }
            }

            if (hasSameColorNeighs)
            {
                res[i][j] = 0;
            }
        }
    }

    for (int i = 1; i <= r; i++)
    {
        for (int j = 1; j <= c; j++)
        {
            queue<pair<int, int>> q;
            if (res[i][j] >= 0)
            {
                q.push({i, j});
            }

            while (!q.empty())
            {
                auto p = q.front();
                q.pop();
                for (auto dir : dirs)
                {
                    int ii = dir.first + p.first;
                    int jj = dir.second + p.second;

                    if (res[ii][jj] < 0)
                    {
                        res[ii][jj] = abs(dir.first) + abs(dir.second) + res[p.first][p.second];
                        q.push({ii, jj});
                    }
                }
            }
        }
    }

    // for (int i = 1; i <= r; i++)
    // {
    //     for (int j = 1; j <= c; j++)
    //     {
    //         if (res[i][j] >= 0)
    //         {
    //             continue;
    //         }

    //         map<pair<int, int>, int> was;
    //         queue<pair<int, int>> q;
    //         q.push({i, j});
    //         was[{i, j}] = true;
    //         while (!q.empty())
    //         {
    //             auto p = q.front();
    //             q.pop();

    //             int ii = p.first;
    //             int jj = p.second;
    //             auto v = res[ii][jj];
    //             if (v == 0)
    //             {
    //                 res[i][j] = abs(i - ii) + abs(j - jj);
    //                 break;
    //             }

    //             for (auto dir : dirs)
    //             {
    //                 int iii = ii + dir.first;
    //                 int jjj = jj +
    //                           dir.second;

    //                 if (!was[{iii, jjj}] && iii >= 1 && jjj >= 1 && iii <= r && jjj <= c)
    //                 {
    //                     was[{iii, jjj}] = true;
    //                     q.push({iii, jjj});
    //                 }
    //             }
    //         }
    //     }
    // }

    fo(tt, t)
    {
        int i, j;
        long long p;
        cin >> i >> j >> p;

        long long r = res[i][j];

        if (r < 0 || r >= p)
        {
            cout << a[i][j] << endl;
        }
        else
        {
            p -= r;
            p += a[i][j];
            cout << (p % 2) << endl;
            // cout << (res[i][j]) << endl;
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll asize=1003;
bool yeet=0;
const ll x[4]={-1,0,1,0},y[4]={0,1,0,-1};
ll n,m;
char a[asize][asize];
ll dfw[asize][asize];
set<pair<ll,ll>>frontiers;
queue<tuple<ll,ll,ll>>que;

void dfs(ll pi,ll pj,char t){
	for(ll i=0;i<4;i++){
		ll npi=pi+x[i],npj=pj+y[i];
		if(npi>=0&&npi<n&&npj>=0&&npj<m){
			if(yeet||a[npi][npj]==t){
				if(t=='0')a[pi][pj]='2';
				else a[pi][pj]='3';
				yeet=1;
				if(a[npi][npj]==t)dfs(npi,npj,t);
			}
		}
	}
}

void wavify(ll pi,ll pj,ll d){
	for(ll i=0;i<4;i++){
		ll npi=pi+x[i],npj=pj+y[i];
		if(npi>=0&&npi<n&&npj>=0&&npj<m){
			if(a[npi][npj]=='1'||a[npi][npj]=='0'){
				if(d+1<dfw[npi][npj]){
					dfw[npi][npj]=d+1;
					que.emplace(npi,npj,d+1);
				}
			}
		}
	}
}

int main(){
	ios_base::sync_with_stdio(0),cin.tie(NULL);
	ll q;
	cin>>n>>m>>q;
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			cin>>a[i][j];
			dfw[i][j]=1e9;
		}
	}
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			if(a[i][j]=='0'||a[i][j]=='1')dfs(i,j,a[i][j]),yeet=0;
		}
	}
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			if(a[i][j]=='2'||a[i][j]=='3')dfw[i][j]=0,que.emplace(i,j,0);
		}
	}
	while(!que.empty()){
		ll q11,q22,q33;
		tie(q11,q22,q33)=que.front();
		wavify(q11,q22,q33);
		que.pop();
	}
	for(ll i=0;i<q;i++){
		ll ix,jx,t;
		cin>>ix>>jx>>t;
		ix--,jx--;
		ll mt=t%2;
		if(a[ix][jx]=='2'){
			if(mt==0)cout<<"0\n";
			else cout<<"1\n";
		}
		else if(a[ix][jx]=='3'){
			if(mt==0)cout<<"1\n";
			else cout<<"0\n";
		}
		else if(a[ix][jx]=='0'){
			if(t<=dfw[ix][jx])cout<<"0\n";
			else if((t-dfw[ix][jx])%2==0)cout<<"1\n";
			else cout<<"0\n";
		}
		else if(a[ix][jx]=='1'){
			if(t<=dfw[ix][jx])cout<<"1\n";
			else if((t-dfw[ix][jx])%2==0)cout<<"0\n";
			else cout<<"1\n";
		}
		}
	}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll asize=1003;
bool yeet=0;
const ll x[4]={-1,0,1,0},y[4]={0,1,0,-1};
ll n,m;
char a[asize][asize];
ll dfw[asize][asize];
set<pair<ll,ll>>frontiers;
queue<tuple<ll,ll,ll>>que;

void dfs(ll pi,ll pj,char t){
	for(ll i=0;i<4;i++){
		ll npi=pi+x[i],npj=pj+y[i];
		if(npi>=0&&npi<n&&npj>=0&&npj<m){
			if(yeet||a[npi][npj]==t){
				if(t=='0')a[pi][pj]='2';
				else a[pi][pj]='3';
				yeet=1;
				if(a[npi][npj]==t)dfs(npi,npj,t);
			}
		}
	}
}

void wavify(ll pi,ll pj,ll d){
	for(ll i=0;i<4;i++){
		ll npi=pi+x[i],npj=pj+y[i];
		if(npi>=0&&npi<n&&npj>=0&&npj<m){
			if(a[npi][npj]=='1'||a[npi][npj]=='0'){
				if(d+1<dfw[npi][npj]){
					dfw[npi][npj]=d+1;
					que.emplace(npi,npj,d+1);
				}
			}
		}
	}
}

int main(){
	ios_base::sync_with_stdio(0),cin.tie(NULL);
	ll q;
	cin>>n>>m>>q;
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			cin>>a[i][j];
			dfw[i][j]=1e9;
		}
	}
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			if(a[i][j]=='0'||a[i][j]=='1')dfs(i,j,a[i][j]),yeet=0;
		}
	}
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			if(a[i][j]=='2'||a[i][j]=='3')dfw[i][j]=0,que.emplace(i,j,0);
		}
	}
	while(!que.empty()){
		ll q11,q22,q33;
		tie(q11,q22,q33)=que.front();
		wavify(q11,q22,q33);
		que.pop();
	}
	for(ll i=0;i<q;i++){
		ll ix,jx,t;
		cin>>ix>>jx>>t;
		ix--,jx--;
		ll mt=t%2;
		if(a[ix][jx]=='2'){
			if(mt==0)cout<<"0\n";
			else cout<<"1\n";
		}
		else if(a[ix][jx]=='3'){
			if(mt==0)cout<<"1\n";
			else cout<<"0\n";
		}
		else if(a[ix][jx]=='0'){
			if(t<dfw[ix][jx])cout<<"0\n";
			else if((t-dfw[ix][jx])%2==0)cout<<"0\n";
			else cout<<"1\n";
		}
		else if(a[ix][jx]=='1'){
			if(t<dfw[ix][jx])cout<<"1\n";
			else if((t-dfw[ix][jx])%2==0)cout<<"1\n";
			else cout<<"0\n";
		}
		}
	}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll asize=103;
bool yeet=0;
const ll x[4]={-1,0,1,0},y[4]={0,1,0,-1};
ll n,m;
char a[asize][asize];
ll dfw[asize][asize];
set<pair<ll,ll>>frontiers;

void dfs(ll pi,ll pj,char t){
	for(ll i=0;i<4;i++){
		ll npi=pi+x[i],npj=pj+y[i];
		if(npi>=0&&npi<n&&npj>=0&&npj<m){
			if(yeet||a[npi][npj]==t){
				if(t=='0')a[pi][pj]='2';
				else a[pi][pj]='3';
				yeet=1;
				if(a[npi][npj]==t)dfs(npi,npj,t);
			}
		}
	}
}

void wavify(ll pi,ll pj,ll d){
	for(ll i=0;i<4;i++){
		ll npi=pi+x[i],npj=pj+y[i];
		if(npi>=0&&npi<n&&npj>=0&&npj<m){
			if(a[npi][npj]=='1'||a[npi][npj]=='0'){
				if(d+1<dfw[npi][npj]){
					dfw[npi][npj]=d+1;
					wavify(npi,npj,d+1);
				}
			}
		}
	}
}

int main(){
//	ios_base::sync_with_stdio(0),cin.tie(NULL);
	ll q;
	cin>>n>>m>>q;
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			cin>>a[i][j];
			dfw[i][j]=1e9;
		}
	}
//	cout<<"input d0ne";
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			if(a[i][j]=='0'||a[i][j]=='1')dfs(i,j,a[i][j]),yeet=0;
		}
	}
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			if(a[i][j]=='2'||a[i][j]=='3')dfw[i][j]=0,wavify(i,j,0);
		}
	}
	for(ll i=0;i<q;i++){
		ll ix,jx,t;
		cin>>ix>>jx>>t;
		ix--,jx--;
		ll mt=t%2;
//		cout<<"thing here:"<<a[ix][jx]<<"\n";
		if(a[ix][jx]=='2'){
			if(mt==0)cout<<"0\n";
			else cout<<"1\n";
		}
		else if(a[ix][jx]=='3'){
			if(mt==0)cout<<"1\n";
			else cout<<"0\n";
		}
		else if(a[ix][jx]=='0'){
			if(t<dfw[ix][jx])cout<<"0\n";
			else if((t-dfw[ix][jx])%2==0)cout<<"0\n";
			else cout<<"1\n";
		}
		else if(a[ix][jx]=='1'){
			if(t<dfw[ix][jx])cout<<"1\n";
			else if((t-dfw[ix][jx])%2==0)cout<<"1\n";
			else cout<<"0\n";
		}
		}
	}
/*	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			cout<<dfw[i][j];
		}
		cout<<"\n";
	}*/


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll asize=1003;
bool yeet=0;
const ll x[4]={-1,0,1,0},y[4]={0,1,0,-1};
ll n,m;
char a[asize][asize];
ll dfw[asize][asize];
set<pair<ll,ll>>frontiers;

void dfs(ll pi,ll pj,char t){
	for(ll i=0;i<4;i++){
		ll npi=pi+x[i],npj=pj+y[i];
		if(npi>=0&&npi<n&&npj>=0&&npj<m){
			if(yeet||a[npi][npj]==t){
				if(t=='0')a[pi][pj]='2';
				else a[pi][pj]='3';
				yeet=1;
				if(a[npi][npj]==t)dfs(npi,npj,t);
			}
		}
	}
}

void wavify(ll pi,ll pj,ll d){
	for(ll i=0;i<4;i++){
		ll npi=pi+x[i],npj=pj+y[i];
		if(npi>=0&&npi<n&&npj>=0&&npj<m){
			if(a[npi][npj]=='1'||a[npi][npj]=='0'){
				if(d+1<dfw[npi][npj]){
					dfw[npi][npj]=d+1;
					wavify(npi,npj,d+1);
				}
			}
		}
	}
}

int main(){
	ios_base::sync_with_stdio(0),cin.tie(NULL);
	ll q;
	cin>>n>>m>>q;
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			cin>>a[i][j];
			dfw[i][j]=1e9;
		}
	}
//	cout<<"input d0ne";
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			if(a[i][j]=='0'||a[i][j]=='1')dfs(i,j,a[i][j]),yeet=0;
		}
	}
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			if(a[i][j]=='2'||a[i][j]=='3')dfw[i][j]=0,wavify(i,j,0);
		}
	}
	for(ll i=0;i<q;i++){
		ll ix,jx,t;
		cin>>ix>>jx>>t;
		ix--,jx--;
		ll mt=t%2;
//		cout<<"thing here:"<<a[ix][jx]<<"\n";
		if(a[ix][jx]=='2'){
			if(mt==0)cout<<"0\n";
			else cout<<"1\n";
		}
		else if(a[ix][jx]=='3'){
			if(mt==0)cout<<"1\n";
			else cout<<"0\n";
		}
		else if(a[ix][jx]=='0'){
			if(t<dfw[ix][jx])cout<<"0\n";
			else if((t-dfw[ix][jx])%2==0)cout<<"0\n";
			else cout<<"1\n";
		}
		else if(a[ix][jx]=='1'){
			if(t<dfw[ix][jx])cout<<"1\n";
			else if((t-dfw[ix][jx])%2==0)cout<<"1\n";
			else cout<<"0\n";
		}
		}
	}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll asize=1003;
bool yeet=0;
const ll x[4]={-1,0,1,0},y[4]={0,1,0,-1};
ll n,m;
char a[asize][asize];
ll dfw[asize][asize];
set<pair<ll,ll>>frontiers;
queue<tuple<ll,ll,ll>>que;

void dfs(ll pi,ll pj,char t){
	for(ll i=0;i<4;i++){
		ll npi=pi+x[i],npj=pj+y[i];
		if(npi>=0&&npi<n&&npj>=0&&npj<m){
			if(yeet||a[npi][npj]==t){
				if(t=='0')a[pi][pj]='2';
				else a[pi][pj]='3';
				yeet=1;
				if(a[npi][npj]==t)dfs(npi,npj,t);
			}
		}
	}
}

void wavify(ll pi,ll pj,ll d){
	for(ll i=0;i<4;i++){
		ll npi=pi+x[i],npj=pj+y[i];
		if(npi>=0&&npi<n&&npj>=0&&npj<m){
			if(a[npi][npj]=='1'||a[npi][npj]=='0'){
				if(d+1<dfw[npi][npj]){
					dfw[npi][npj]=d+1;
					que.emplace(npi,npj,d+1);
				}
			}
		}
	}
}

int main(){
	ios_base::sync_with_stdio(0),cin.tie(NULL);
	ll q;
	cin>>n>>m>>q;
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			cin>>a[i][j];
			dfw[i][j]=1e18+1;
		}
	}
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			if(a[i][j]=='0'||a[i][j]=='1')dfs(i,j,a[i][j]),yeet=0;
		}
	}
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			if(a[i][j]=='2'||a[i][j]=='3')dfw[i][j]=0,que.emplace(i,j,0);
		}
	}
	while(!que.empty()){
		ll q11,q22,q33;
		tie(q11,q22,q33)=que.front();
		wavify(q11,q22,q33);
		que.pop();
	}
	for(ll i=0;i<q;i++){
		ll ix,jx,t;
		cin>>ix>>jx>>t;
		ix--,jx--;
		ll mt=t%2;
		if(a[ix][jx]=='2'){
			if(mt==0)cout<<"0\n";
			else cout<<"1\n";
		}
		else if(a[ix][jx]=='3'){
			if(mt==0)cout<<"1\n";
			else cout<<"0\n";
		}
		else if(a[ix][jx]=='0'){
			if(t<=dfw[ix][jx])cout<<"0\n";
			else if((t-dfw[ix][jx])%2==0)cout<<"0\n";
			else cout<<"1\n";
		}
		else if(a[ix][jx]=='1'){
			if(t<=dfw[ix][jx])cout<<"1\n";
			else if((t-dfw[ix][jx])%2==0)cout<<"1\n";
			else cout<<"0\n";
		}
		}
	}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll asize=1003;
bool yeet=0;
const ll x[4]={-1,0,1,0},y[4]={0,1,0,-1};
ll n,m;
char a[asize][asize];
ll dfw[asize][asize];
set<pair<ll,ll>>frontiers;

void dfs(ll pi,ll pj,char t){
	for(ll i=0;i<4;i++){
		ll npi=pi+x[i],npj=pj+y[i];
		if(npi>=0&&npi<n&&npj>=0&&npj<m){
			if(yeet||a[npi][npj]==t){
				if(t=='0')a[pi][pj]='2';
				else a[pi][pj]='3';
				yeet=1;
				if(a[npi][npj]==t)dfs(npi,npj,t);
			}
		}
	}
}

void wavify(ll pi,ll pj,ll d){
	for(ll i=0;i<4;i++){
		ll npi=pi+x[i],npj=pj+y[i];
		if(npi>=0&&npi<n&&npj>=0&&npj<m){
			if(a[npi][npj]=='1'||a[npi][npj]=='0'){
				if(d+1<dfw[npi][npj]){
					dfw[npi][npj]=d+1;
					wavify(npi,npj,d+1);
				}
			}
		}
	}
}

int main(){
//	ios_base::sync_with_stdio(0),cin.tie(NULL);
	ll q;
	cin>>n>>m>>q;
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			cin>>a[i][j];
			dfw[i][j]=1e9;
		}
	}
//	cout<<"input d0ne";
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			if(a[i][j]=='0'||a[i][j]=='1')dfs(i,j,a[i][j]),yeet=0;
		}
	}
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			if(a[i][j]=='2'||a[i][j]=='3')dfw[i][j]=0,wavify(i,j,0);
		}
	}
	for(ll i=0;i<q;i++){
		ll ix,jx,t;
		cin>>ix>>jx>>t;
		ix--,jx--;
		ll mt=t%2;
//		cout<<"thing here:"<<a[ix][jx]<<"\n";
		if(a[ix][jx]=='2'){
			if(mt==0)cout<<"0\n";
			else cout<<"1\n";
		}
		else if(a[ix][jx]=='3'){
			if(mt==0)cout<<"1\n";
			else cout<<"0\n";
		}
		else if(a[ix][jx]=='0'){
			if(t<dfw[ix][jx])cout<<"0\n";
			else if((t-dfw[ix][jx])%2==0)cout<<"0\n";
			else cout<<"1\n";
		}
		else if(a[ix][jx]=='1'){
			if(t<dfw[ix][jx])cout<<"1\n";
			else if((t-dfw[ix][jx])%2==0)cout<<"1\n";
			else cout<<"0\n";
		}
		}
	}
/*	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			cout<<dfw[i][j];
		}
		cout<<"\n";
	}*/


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll asize=1003;
bool yeet=0;
const ll x[4]={-1,0,1,0},y[4]={0,1,0,-1};
ll n,m;
char a[asize][asize];
ll dfw[asize][asize];
set<pair<ll,ll>>frontiers;
queue<tuple<ll,ll,ll>>que;

void dfs(ll pi,ll pj,char t){
	for(ll i=0;i<4;i++){
		ll npi=pi+x[i],npj=pj+y[i];
		if(npi>=0&&npi<n&&npj>=0&&npj<m){
			if(yeet||a[npi][npj]==t){
				if(t=='0')a[pi][pj]='2';
				else a[pi][pj]='3';
				yeet=1;
				if(a[npi][npj]==t)dfs(npi,npj,t);
			}
		}
	}
}

void wavify(ll pi,ll pj,ll d){
	for(ll i=0;i<4;i++){
		ll npi=pi+x[i],npj=pj+y[i];
		if(npi>=0&&npi<n&&npj>=0&&npj<m){
			if(a[npi][npj]=='1'||a[npi][npj]=='0'){
				if(d+1<dfw[npi][npj]){
					dfw[npi][npj]=d+1;
					que.emplace(npi,npj,d+1);
				}
			}
		}
	}
}

int main(){
	ios_base::sync_with_stdio(0),cin.tie(NULL);
	ll q;
	cin>>n>>m>>q;
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			cin>>a[i][j];
			dfw[i][j]=1e9;
		}
	}
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			if(a[i][j]=='0'||a[i][j]=='1')dfs(i,j,a[i][j]),yeet=0;
		}
	}
	for(ll i=0;i<n;i++){
		for(ll j=0;j<m;j++){
			if(a[i][j]=='2'||a[i][j]=='3')dfw[i][j]=0,que.emplace(i,j,0);
		}
	}
	while(!que.empty()){
		ll q11,q22,q33;
		tie(q11,q22,q33)=que.front();
		wavify(q11,q22,q33);
		que.pop();
	}
	for(ll i=0;i<q;i++){
		ll ix,jx,t;
		cin>>ix>>jx>>t;
		ix--,jx--;
		ll mt=t%2;
		if(a[ix][jx]=='2'){
			if(mt==0)cout<<"0\n";
			else cout<<"1\n";
		}
		else if(a[ix][jx]=='3'){
			if(mt==0)cout<<"1\n";
			else cout<<"0\n";
		}
		else if(a[ix][jx]=='0'){
			if(t<=dfw[ix][jx])cout<<"0\n";
			else if((t-dfw[ix][jx])%2==0)cout<<"0\n";
			else cout<<"1\n";
		}
		else if(a[ix][jx]=='1'){
			if(t<=dfw[ix][jx])cout<<"1\n";
			else if((t-dfw[ix][jx])%2==0)cout<<"1\n";
			else cout<<"0\n";
		}
		}
	}


#include<bits/stdc++.h>
#define forn(i,n) for(int i = 0; i<int(n); i++)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<int, int>;

int n,m,t;
string s[1000];
int d[1000][1000];
vector<pair<int,int>> v = {{1,0}, {-1,0}, {0,1}, {0,-1}};

void bfs()
{
    queue<pair<int, int>> q;
    forn(i,n) forn(j,m)
    {
        bool flag = 0;
        d[i][j] = INT_MAX;
        for(auto itr:v)
        {
            if(i+itr.first < n && i+itr.first >= 0 &&
               j+itr.second < m && j+itr.second >= 0 &&
               s[i+itr.first][j+itr.second] == s[i][j])
                    flag = 1;
        }
        if(flag){q.push({i,j}); d[i][j] = 1;}
    }

    while(!q.empty())
    {
        pr t = q.front();
        q.pop();

        for(auto itr:v)
        {
            pr sum = {t.first+itr.first, t.second+itr.second};

            if(sum.first < n && sum.first >= 0 &&
               sum.second < m && sum.second >= 0 &&
               d[sum.first][sum.second] == INT_MAX)
                {
                    q.push(sum);
                    d[sum.first][sum.second] = d[t.first][t.second]+1;
                }
        }
    }
}

int main()
{

    cin>>n>>m>>t;
    forn(i,n) cin>>s[i];
    bfs();

    while(t--)
    {
        int i,j,p;
        cin>>i>>j>>p;
        i--;
        j--;

        if(p < d[i][j] || (p-d[i][j])%2) cout<<s[i][j]<<"\n";
        else cout<<!(s[i][j]-'0')<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
#define forn(i,n) for(int i = 0; i<int(n); i++)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<int, int>;

int n,m,t;
string s[1000];
int d[1000][1000];
vector<pair<int,int>> v = {{1,0}, {-1,0}, {0,1}, {0,-1}};

void bfs()
{
    queue<pair<int, int>> q;
    forn(i,n) forn(j,m)
    {
        bool flag = 0;
        d[i][j] = -1;
        for(auto itr:v)
        {
            if(i+itr.first < n && i+itr.first >= 0 &&
               j+itr.second < m && j+itr.second >= 0 &&
               s[i+itr.first][j+itr.second] == s[i][j])
                    flag = 1;
        }
        if(flag){q.push({i,j}); d[i][j] = 1;}
    }

    while(!q.empty())
    {
        pr t = q.front();
        q.pop();

        for(auto itr:v)
        {
            pr sum = {t.first+itr.first, t.second+itr.second};

            if(sum.first < n && sum.first >= 0 &&
               sum.second < m && sum.second >= 0 &&
               d[sum.first][sum.second] == -1)
                {
                    q.push(sum);
                    d[sum.first][sum.second] = d[t.first][t.second]+1;
                }
        }
    }
}

int main()
{

    cin>>n>>m>>t;
    forn(i,n) cin>>s[i];
    bfs();
    //forn(i,n){ forn(j,m) cout<<d[i][j]<<" "; cout<<"\n";}

    /*cout<<s[0][0];
    for(auto itr:v)
        if(itr.first < n && itr.first >= 0 &&
           itr.second < m && itr.second >= 0 &&
           s[itr.first][itr.second] == s[0][0])
           {
               cout<<s[0][0]<<" "<<s[itr.first][itr.second]<<"\n";
           }
    */

    while(t--)
    {
        int i,j;
        ll p;
        cin>>i>>j>>p;
        i--;
        j--;

        if(d[i][j] == -1 || p < d[i][j] || (p-d[i][j])%2) cout<<s[i][j]<<"\n";
        else cout<<!(s[i][j]-'0')<<"\n";
    }
    return 0;
}

/*

3 3 3
010
101
010

*/


#include<bits/stdc++.h>
#define forn(i,n) for(int i = 0; i<int(n); i++)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<int, int>;

int n,m,t;
string s[1000];
int d[1000][1000];
vector<pair<int,int>> v = {{1,0}, {-1,0}, {0,1}, {0,-1}};

void bfs()
{
    queue<pair<int, int>> q;
    forn(i,n) forn(j,m)
    {
        bool flag = 0;
        d[i][j] = INT_MAX;
        for(auto itr:v)
        {
            if(i+itr.first < n && i+itr.first >= 0 &&
               j+itr.second < m && j+itr.second >= 0 &&
               s[i+itr.first][j+itr.second] == s[i][j])
                    flag = 1;
        }
        if(flag){q.push({i,j}); d[i][j] = 1;}
    }

    while(!q.empty())
    {
        pr t = q.front();
        q.pop();

        for(auto itr:v)
        {
            pr sum = {t.first+itr.first, t.second+itr.second};

            if(sum.first < n && sum.first >= 0 &&
               sum.second < m && sum.second >= 0 &&
               d[sum.first][sum.second] == INT_MAX)
                {
                    q.push(sum);
                    d[sum.first][sum.second] = d[t.first][t.second]+1;
                }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

    cin>>n>>m>>t;
    forn(i,n) cin>>s[i];
    bfs();
    forn(i,n){ forn(j,m) cout<<d[i][j]<<" "; cout<<"\n";}

    while(t--)
    {
        int i,j,p;
        cin>>i>>j>>p;
        i--;
        j--;

        if(p < d[i][j] || (p-d[i][j])%2) cout<<s[i][j]<<"\n";
        else cout<<!(s[i][j]-'0')<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
#define forn(i,n) for(int i = 0; i<int(n); i++)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<int, int>;

int n,m,t;
string s[1000];
int d[1000][1000];
vector<pair<int,int>> v = {{1,0}, {-1,0}, {0,1}, {0,-1}};

void bfs()
{
    queue<pair<int, int>> q;
    forn(i,n) forn(j,m)
    {
        bool flag = 0;
        d[i][j] = INT_MAX;
        for(auto itr:v)
        {
            if(i+itr.first < n && i+itr.first >= 0 &&
               j+itr.second < m && j+itr.second >= 0 &&
               s[i+itr.first][j+itr.second] == s[i][j])
                    flag = 1;
        }
        if(flag){q.push({i,j}); d[i][j] = 1;}
    }

    while(!q.empty())
    {
        pr t = q.front();
        q.pop();

        for(auto itr:v)
        {
            pr sum = {t.first+itr.first, t.second+itr.second};

            if(sum.first < n && sum.first >= 0 &&
               sum.second < m && sum.second >= 0 &&
               d[sum.first][sum.second] == INT_MAX)
                {
                    q.push(sum);
                    d[sum.first][sum.second] = d[t.first][t.second]+1;
                }
        }
    }
}

int main()
{

    cin>>n>>m>>t;
    forn(i,n) cin>>s[i];
    bfs();
    //forn(i,n){ forn(j,m) cout<<d[i][j]<<" "; cout<<"\n";}

    /*cout<<s[0][0];
    for(auto itr:v)
        if(itr.first < n && itr.first >= 0 &&
           itr.second < m && itr.second >= 0 &&
           s[itr.first][itr.second] == s[0][0])
           {
               cout<<s[0][0]<<" "<<s[itr.first][itr.second]<<"\n";
           }
    */

    while(t--)
    {
        int i,j;
        ll p;
        cin>>i>>j>>p;
        i--;
        j--;

        if(p < d[i][j] || (p-d[i][j])%2) cout<<s[i][j]<<"\n";
        else cout<<!(s[i][j]-'0')<<"\n";
    }
    return 0;
}

/*

3 3 3
010
101
010

*/


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long int
#define MOD 1000000007
#define INF 100000000000000 //INF - 1e14
#define vi vector<ll>
#define pb push_back
#define mpl map<ll,ll>
#define ff first
#define ss second
#define pll pair<ll,ll>
#define vpll vector<pair<ll,ll> >
#define fer(i, a, n) for( ll i = a; i <= n; i++)
#define fdr(i, a, n) for( ll i = a; i >= n; i--)
#define invec(n,v) vi v(n); fer(i,0,n-1) cin >> v[i]
#define insvec(n,v) rin(n); vi v(n); fer(i,0,n-1) cin >> v[i]
#define MK2DV(v,row,col,val) vector<vector<ll> > v(row, vector<ll>(col,val))
#define matrix vector<vector<ll> >
#define printv(v,i,j) for(ll it =i; it <= j; it++) cout << v[it] << ' '; cout << '\n';
#define printpartition cout << "-------------------------------------------------------------" << endl;
#define printmat(mat,row,col) cout << #mat << "::" << endl; fer(i,0,row-1)fer(j,0,col-1)cout << mat[i][j] << " \n"[j == col-1]; printpartition;
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i : x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
template <typename T> inline void inp(T &any) {cin >> any;} template <typename T, typename... U>inline void inp(T &a, U &...b) {cin >> a; inp(b...);}
#define rin(args...) ll args; inp(args);
#ifndef ONLINE_JUDGE
#define debug(x...) cout << "[" << #x << "] = ["; _print(x)
#define matdeb(mat,row,col) printmat(mat,row,col);
#define debugv(v,i,j) printv(v,i,j); printpartition;
#else
#define debug(x...)
#define matdeb(mat,row,col)
#define debugv(v,i,j)
#endif
ll fast_pow(ll base, ll exp, ll M = MOD) { base = base % M; ll result = 1; while (exp > 0) { if (exp % 2 == 1) result = (result * base) % M; base = (base * base) % M; exp = exp / 2; } return result; }
ll modInverse(ll A, ll M = MOD) { return fast_pow(A, M - 2, M); } // only when M is prime
void coordinatecompress(vector<ll> &arr) { ll n = arr.size(); vector<pair<ll, ll> > temparr(n); fer(i, 0, n - 1) temparr[i] = {arr[i], i}; sort(temparr.begin(), temparr.end()); arr[temparr[0].second] = 0; fer(i, 1, n - 1) arr[temparr[i].second] = arr[temparr[i - 1].second] + (temparr[i - 1].first != temparr[i].first); }
string yes = "Yes", no = "No";

bool mat[1000][1000];
int val[1000][1000];
int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("intt.txt", "r", stdin);
	freopen("outt.txt", "w", stdout);
#endif
	ll n, m, p;
	cin >> n >> m >> p;
	fer(i, 0, n - 1) {
		fer(j, 0, m - 1) {
			char ch;
			cin >> ch;
			mat[i][j] = (ch - '0');
		}
	}

	cout << "1\n1\n1\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int mat[1000][1000];
int dist[1000][1000];
int main() {
  int n, m, t;
  cin >> n >> m >> t;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      char ch;
      cin >> ch;
      mat[i][j] = ch - '0';
      dist[i][j] = -1;
    }
  }
  queue<pair<int, int > > q;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      for (pair<int, int> p : { make_pair(i - 1, j), make_pair(i + 1, j), make_pair(i, j - 1), make_pair(i, j + 1) }) {
        int ii = p.first, jj = p.second;
        if (ii < 0 || ii >= n) continue;
        if (jj < 0 || jj >= m) continue;
        if (mat[ii][jj] == mat[i][j]) {
          q.push({i, j});
          dist[i][j] = 0;
          break;
        }
      }
    }
  }
  while (!q.empty()) {
    pair<int, int> v = q.front();
    int i = v.first, j = v.second;
    q.pop();
    for (pair<int, int> p : { make_pair(i - 1, j), make_pair(i + 1, j), make_pair(i, j - 1), make_pair(i, j + 1) }) {
      int ii = p.first, jj = p.second;
      if (ii < 0 || ii >= n) continue;
      if (jj < 0 || jj >= m) continue;
      if (dist[ii][jj] != -1) continue;
      dist[ii][jj] = dist[i][j] + 1;
      q.push({ii, jj});
    }
  }
  int i, j;
  long long int d, qn;
  while (t--) {
    cin >> i >> j >> qn;
    i--; j--;
    d = qn - (long long int)dist[i][j];
    if (dist[i][j] == -1 || d < 0)
      cout << mat[i][j] << '\n';
    else if (d & 1LL)
      cout << !mat[i][j] << '\n';
    else
      cout << mat[i][j] << '\n';
  }
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long int
#define MOD 1000000007
#define INF 100000000000000 //INF - 1e14
#define vi vector<ll>
#define pb push_back
#define mpl map<ll,ll>
#define ff first
#define ss second
#define pll pair<ll,ll>
#define vpll vector<pair<ll,ll> >
#define fer(i, a, n) for( ll i = a; i <= n; i++)
#define fdr(i, a, n) for( ll i = a; i >= n; i--)
#define invec(n,v) vi v(n); fer(i,0,n-1) cin >> v[i]
#define insvec(n,v) rin(n); vi v(n); fer(i,0,n-1) cin >> v[i]
#define MK2DV(v,row,col,val) vector<vector<ll> > v(row, vector<ll>(col,val))
#define matrix vector<vector<ll> >
#define printv(v,i,j) for(ll it =i; it <= j; it++) cout << v[it] << ' '; cout << '\n';
#define printpartition cout << "-------------------------------------------------------------" << endl;
#define printmat(mat,row,col) cout << #mat << "::" << endl; fer(i,0,row-1)fer(j,0,col-1)cout << mat[i][j] << " \n"[j == col-1]; printpartition;
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i : x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
template <typename T> inline void inp(T &any) {cin >> any;} template <typename T, typename... U>inline void inp(T &a, U &...b) {cin >> a; inp(b...);}
#define rin(args...) ll args; inp(args);
#ifndef ONLINE_JUDGE
#define debug(x...) cout << "[" << #x << "] = ["; _print(x)
#define matdeb(mat,row,col) printmat(mat,row,col);
#define debugv(v,i,j) printv(v,i,j); printpartition;
#else
#define debug(x...)
#define matdeb(mat,row,col)
#define debugv(v,i,j)
#endif
ll fast_pow(ll base, ll exp, ll M = MOD) { base = base % M; ll result = 1; while (exp > 0) { if (exp % 2 == 1) result = (result * base) % M; base = (base * base) % M; exp = exp / 2; } return result; }
ll modInverse(ll A, ll M = MOD) { return fast_pow(A, M - 2, M); } // only when M is prime
void coordinatecompress(vector<ll> &arr) { ll n = arr.size(); vector<pair<ll, ll> > temparr(n); fer(i, 0, n - 1) temparr[i] = {arr[i], i}; sort(temparr.begin(), temparr.end()); arr[temparr[0].second] = 0; fer(i, 1, n - 1) arr[temparr[i].second] = arr[temparr[i - 1].second] + (temparr[i - 1].first != temparr[i].first); }

string yes = "Yes", no = "No";
int mat[1000][1000];
int dist[1000][1000];
int n, m;

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("intt.txt", "r", stdin);
	freopen("outt.txt", "w", stdout);
#endif
	int t;
	cin >> n >> m >> t;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			char ch;
			cin >> ch;
			mat[i][j] = ch - '0';
			dist[i][j] = -1;
		}
	}
	matdeb(mat, n, m);
	queue<pair<int, int > > q;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			for (pair<int, int> p : { make_pair(i - 1, j), make_pair(i + 1, j), make_pair(i, j - 1), make_pair(i, j + 1) }) {
				int ii = p.first, jj = p.second;
				if (ii < 0 || ii >= n) continue;
				if (jj < 0 || jj >= m) continue;
				if (mat[ii][jj] == mat[i][j]) {
					q.push({i, j});
					dist[i][j] = 0;
					break;
				}
			}
		}
	}
	while (!q.empty()) {
		pair<int, int> v = q.front();
		int i = v.first, j = v.second;
		q.pop();
		for (pair<int, int> p : { make_pair(i - 1, j), make_pair(i + 1, j), make_pair(i, j - 1), make_pair(i, j + 1) }) {
			int ii = p.first, jj = p.second;
			if (ii < 0 || ii >= n) continue;
			if (jj < 0 || jj >= m) continue;
			if (dist[ii][jj] != -1) continue;
			dist[ii][jj] = dist[i][j] + 1;
			q.push({ii, jj});
		}
	}
	int qi, qj, qn, diff;
	while (t--) {
		cin >> qi >> qj >> qn;
		qi--; qj--;
		if (dist[qi][qj] == -1)
			cout << mat[qi][qj] << '\n';
		else if (dist[qi][qj] >= qn)
			cout << mat[qi][qj] << '\n';
		else {
			diff = qn - dist[qi][qj];
			if (diff & 1)
				cout << !mat[qi][qj] << '\n';
			else
				cout << mat[qi][qj] << '\n';
		}
	}

	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long int
#define MOD 1000000007
#define INF 100000000000000 //INF - 1e14
#define vi vector<ll>
#define pb push_back
#define mpl map<ll,ll>
#define ff first
#define ss second
#define pll pair<ll,ll>
#define vpll vector<pair<ll,ll> >
#define fer(i, a, n) for( ll i = a; i <= n; i++)
#define fdr(i, a, n) for( ll i = a; i >= n; i--)
#define invec(n,v) vi v(n); fer(i,0,n-1) cin >> v[i]
#define insvec(n,v) rin(n); vi v(n); fer(i,0,n-1) cin >> v[i]
#define MK2DV(v,row,col,val) vector<vector<ll> > v(row, vector<ll>(col,val))
#define matrix vector<vector<ll> >
#define printv(v,i,j) for(ll it =i; it <= j; it++) cout << v[it] << ' '; cout << '\n';
#define printpartition cout << "-------------------------------------------------------------" << endl;
#define printmat(mat,row,col) cout << #mat << "::" << endl; fer(i,0,row-1)fer(j,0,col-1)cout << mat[i][j] << " \n"[j == col-1]; printpartition;
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i : x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
template <typename T> inline void inp(T &any) {cin >> any;} template <typename T, typename... U>inline void inp(T &a, U &...b) {cin >> a; inp(b...);}
#define rin(args...) ll args; inp(args);
#ifndef ONLINE_JUDGE
#define debug(x...) cout << "[" << #x << "] = ["; _print(x)
#define matdeb(mat,row,col) printmat(mat,row,col);
#define debugv(v,i,j) printv(v,i,j); printpartition;
#else
#define debug(x...)
#define matdeb(mat,row,col)
#define debugv(v,i,j)
#endif
ll fast_pow(ll base, ll exp, ll M = MOD) { base = base % M; ll result = 1; while (exp > 0) { if (exp % 2 == 1) result = (result * base) % M; base = (base * base) % M; exp = exp / 2; } return result; }
ll modInverse(ll A, ll M = MOD) { return fast_pow(A, M - 2, M); } // only when M is prime
void coordinatecompress(vector<ll> &arr) { ll n = arr.size(); vector<pair<ll, ll> > temparr(n); fer(i, 0, n - 1) temparr[i] = {arr[i], i}; sort(temparr.begin(), temparr.end()); arr[temparr[0].second] = 0; fer(i, 1, n - 1) arr[temparr[i].second] = arr[temparr[i - 1].second] + (temparr[i - 1].first != temparr[i].first); }

string yes = "Yes", no = "No";
int mat[1000][1000];
int dist[1000][1000];
int n, m;

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("intt.txt", "r", stdin);
	freopen("outt.txt", "w", stdout);
#endif
	int t;
	cin >> n >> m >> t;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			char ch;
			cin >> ch;
			mat[i][j] = ch - '0';
			dist[i][j] = -1;
		}
	}
	matdeb(mat, n, m);
	queue<pair<int, int > > q;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			for (pair<int, int> p : { make_pair(i - 1, j), make_pair(i + 1, j), make_pair(i, j - 1), make_pair(i, j + 1) }) {
				int ii = p.first, jj = p.second;
				if (ii < 0 || ii >= n) continue;
				if (jj < 0 || jj >= m) continue;
				if (mat[ii][jj] == mat[i][j]) {
					q.push({i, j});
					dist[i][j] = 0;
					break;
				}
			}
		}
	}
	while (!q.empty()) {
		pair<int, int> v = q.front();
		int i = v.first, j = v.second;
		q.pop();
		for (pair<int, int> p : { make_pair(i - 1, j), make_pair(i + 1, j), make_pair(i, j - 1), make_pair(i, j + 1) }) {
			int ii = p.first, jj = p.second;
			if (ii < 0 || ii >= n) continue;
			if (jj < 0 || jj >= m) continue;
			if (dist[ii][jj] != -1) continue;
			dist[ii][jj] = dist[i][j] + 1;
			q.push({ii, jj});
		}
	}
	int qi, qj;
	long long int diff, qn;
	while (t--) {
		cin >> qi >> qj >> qn;
		qi--; qj--;
		if (dist[qi][qj] == -1)
			cout << mat[qi][qj] << '\n';
		else if ((long long int)dist[qi][qj] >= qn)
			cout << mat[qi][qj] << '\n';
		else {
			diff = qn - (long long int)dist[qi][qj];
			if (diff & 1LL)
				cout << !mat[qi][qj] << '\n';
			else
				cout << mat[qi][qj] << '\n';
		}
	}

	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long int
#define MOD 1000000007
#define INF 100000000000000 //INF - 1e14
#define vi vector<ll>
#define pb push_back
#define mpl map<ll,ll>
#define ff first
#define ss second
#define pll pair<ll,ll>
#define vpll vector<pair<ll,ll> >
#define fer(i, a, n) for( ll i = a; i <= n; i++)
#define fdr(i, a, n) for( ll i = a; i >= n; i--)
#define invec(n,v) vi v(n); fer(i,0,n-1) cin >> v[i]
#define insvec(n,v) rin(n); vi v(n); fer(i,0,n-1) cin >> v[i]
#define MK2DV(v,row,col,val) vector<vector<ll> > v(row, vector<ll>(col,val))
#define matrix vector<vector<ll> >
#define printv(v,i,j) for(ll it =i; it <= j; it++) cout << v[it] << ' '; cout << '\n';
#define printpartition cout << "-------------------------------------------------------------" << endl;
#define printmat(mat,row,col) cout << #mat << "::" << endl; fer(i,0,row-1)fer(j,0,col-1)cout << mat[i][j] << " \n"[j == col-1]; printpartition;
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i : x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
template <typename T> inline void inp(T &any) {cin >> any;} template <typename T, typename... U>inline void inp(T &a, U &...b) {cin >> a; inp(b...);}
#define rin(args...) ll args; inp(args);
#ifndef ONLINE_JUDGE
#define debug(x...) cout << "[" << #x << "] = ["; _print(x)
#define matdeb(mat,row,col) printmat(mat,row,col);
#define debugv(v,i,j) printv(v,i,j); printpartition;
#else
#define debug(x...)
#define matdeb(mat,row,col)
#define debugv(v,i,j)
#endif
ll fast_pow(ll base, ll exp, ll M = MOD) { base = base % M; ll result = 1; while (exp > 0) { if (exp % 2 == 1) result = (result * base) % M; base = (base * base) % M; exp = exp / 2; } return result; }
ll modInverse(ll A, ll M = MOD) { return fast_pow(A, M - 2, M); } // only when M is prime
void coordinatecompress(vector<ll> &arr) { ll n = arr.size(); vector<pair<ll, ll> > temparr(n); fer(i, 0, n - 1) temparr[i] = {arr[i], i}; sort(temparr.begin(), temparr.end()); arr[temparr[0].second] = 0; fer(i, 1, n - 1) arr[temparr[i].second] = arr[temparr[i - 1].second] + (temparr[i - 1].first != temparr[i].first); }
string yes = "Yes", no = "No";

bool mat[1000][1000];
int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("intt.txt", "r", stdin);
	freopen("outt.txt", "w", stdout);
#endif
	ll n, m, p;
	cin >> n >> m >> p;
	fer(i, 0, n - 1) {
		fer(j, 0, m - 1) {
			char ch;
			cin >> ch;
			mat[i][j] = (ch - '0');
		}
	}
	cout << "1\n1\n1\n";
	return 0;
}


/*Ankon Ghosh
  KUET,ECE 2K16*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
///#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define inf 1e18
#define MAX 1002
#define mod 1000000007
#define sf(a) scanf("%lld",&a)
#define sff(a,b) scanf("%lld %lld",&a,&b)
#define sfff(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define Case(i) printf("Case %lld: ",i)
#define setpre(number) cout<<fixed<<setprecision(10)<<number<<endl
#define pf(a) printf("%lld\n",a)
#define pff(a,b) printf("%lld %lld\n",a,b)
#define FOR(i,n) for(ll i=0;i<n;i++)
#define FOR1(i,n) for(ll i=1;i<=n;i++)
#define LOOP(x,y) for(ll i=x;i<=y;i++)
#define RFOR(i,n) for(ll i=n-1;i>=0;i--)
#define RFOR1(i,n) for(ll i=n;i>0;i--)
#define RLOOP(x,y) for(ll i=y;i>=x;i--)
#define m_p make_pair
#define ff first
#define ss second
#define p_b push_back
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)
map<ll,ll>mp1;
vector<pair<ll,pair<ll,ll> > >vpp1;
vector<pair<ll,ll> >vp,vp1;
vector<ll>v,v1,v2,fin;
map<pair<ll,ll>,bool>mbb;
map<ll,bool>mb;
///ll n,a,b,c,d,e,f,ta,sum=0,cnt=0,m,l,req,sz,maxi,k;
bool sortbysec(const pair<pair<ll,ll>,ll> &a,const pair<pair<ll,ll>,ll> &b)
{
    return (a.second < b.second);
}
ll grid[MAX+2][MAX+2];
ll dep[MAX+2][MAX+2];
ll n,m,q;
ll x,y,idx;
ll dx[]= {1,0,0,-1};
ll dy[]= {0,1,-1,0};
bool flag=0;
ll cnt=0;
ll alter(ll ch)
{
    if(ch==0)
        return 1;
    else
        return 0;
}
void reset()
{
    FOR(i,MAX)
    {
        FOR(j,MAX) dep[i][j]=inf;
    }
}
/*void bfs(ll i,ll j)
{
    for(ll k=0; k<4; k++)
    {
        ll x=i+dx[k];
        ll y=j+dy[k];
        if(x>=0 and x<n and y>=0 and y<m)
        {
            if(dep[i][j]+1<dep[x][y])
            {
                dep[x][y]=dep[i][j]+1;
                bfs(x,y);
            }
        }
    }
}*/
void bfs(ll i,ll j)
{
    queue<pair<ll,ll> >q;
    q.push(m_p(i,j));
    while(!q.empty())
    {
        ll i=q.front().ff;
        ll j=q.front().ss;
        q.pop();
        for(ll k=0; k<4; k++)
        {
            ll x=i+dx[k];
            ll y=j+dy[k];
            if(x>=0 and x<n and y>=0 and y<m)
            {
                if(dep[i][j]+1<dep[x][y])
                {
                    dep[x][y]=dep[i][j]+1;
                    q.push(m_p(x,y));
                }
            }
        }
    }
}
int main()
{
    reset();
    ///cin>>n>>m>>q;
    sfff(n,m,q);
    FOR(i,n)
    {
        FOR(j,m)
        {
            char ch;
            scanf(" %c",&ch);
            if(ch=='1')
                grid[i][j]=1;
            else
                grid[i][j]=0;
        }
    }
    FOR(i,n)
    {
        FOR(j,m)
        {
            for(ll k=0; k<4; k++)
            {
                ll x=i+dx[k];
                ll y=j+dy[k];
                if(x>=0 and x<n and y>=0 and y<m and grid[i][j]==grid[x][y])
                {
                    dep[i][j]=0;
                    break;
                }
            }
        }
    }
    ll reff;
    FOR(i,n)
    {
        FOR(j,m)
        {
            if(dep[i][j]==0)
                bfs(i,j);
        }
    }
    /*cout<<"check"<<endl;
    FOR(i,n)
    {
        FOR(j,m) cout<<dep[i][j];
        cout<<endl;
    }
    cout<<"done"<<endl;*/
    FOR(i,q)
    {
        ll idx;
        ///cin>>x>>y>>idx;
        sfff(x,y,idx);
        --x;
        --y;
        ll ok=dep[x][y];
        ///cout<<x<<" "<<y<<" "<<ok<<endl;
        if(ok>=idx)
        {
            pf(grid[x][y]);
            ///cout<<grid[x][y]<<endl;
        }
        else
        {
            ll rem=idx-ok;
            ///cout<<rem<<" "<<grid[x][y]<<endl;
            if(rem&1)
                pf(alter(grid[x][y]));
            else
                pf(grid[x][y]);
        }
    }
    return 0;
}


/*Ankon Ghosh
  KUET,ECE 2K16*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
///#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define inf 1e18
#define MAX 1002
#define mod 1000000007
#define sf(a) scanf("%lld",&a)
#define sff(a,b) scanf("%lld %lld",&a,&b)
#define sfff(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define Case(i) printf("Case %lld: ",i)
#define setpre(number) cout<<fixed<<setprecision(10)<<number<<endl
#define pf(a) printf("%lld ",a)
#define pff(a,b) printf("%lld %lld\n",a,b)
#define FOR(i,n) for(ll i=0;i<n;i++)
#define FOR1(i,n) for(ll i=1;i<=n;i++)
#define LOOP(x,y) for(ll i=x;i<=y;i++)
#define RFOR(i,n) for(ll i=n-1;i>=0;i--)
#define RFOR1(i,n) for(ll i=n;i>0;i--)
#define RLOOP(x,y) for(ll i=y;i>=x;i--)
#define m_p make_pair
#define ff first
#define ss second
#define p_b push_back
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)
map<ll,ll>mp1;
vector<pair<ll,pair<ll,ll> > >vpp1;
vector<pair<ll,ll> >vp,vp1;
vector<ll>v,v1,v2,fin;
map<pair<ll,ll>,bool>mbb;
map<ll,bool>mb;
///ll n,a,b,c,d,e,f,ta,sum=0,cnt=0,m,l,req,sz,maxi,k;
bool sortbysec(const pair<pair<ll,ll>,ll> &a,const pair<pair<ll,ll>,ll> &b)
{
    return (a.second < b.second);
}
char grid[MAX+2][MAX+2];
ll dep[MAX+2][MAX+2];
ll n,m,q;
ll x,y,idx;
ll dx[]= {1,0,0,-1};
ll dy[]= {0,1,-1,0};
bool flag=0;
vector<pair<ll,pair<ll,pair<ll,ll> > > >arr;
ll cnt=0;
char alter(char ch)
{
    if(ch=='0')
        return '1';
    else
        return '0';
}
void reset()
{
    FOR(i,MAX)
    {
        FOR(j,MAX) dep[i][j]=inf;
    }
}
void bfs(ll i,ll j)
{
    for(ll k=0; k<4; k++)
    {
        ll x=i+dx[k];
        ll y=j+dy[k];
        if(x>=0 and x<n and y>=0 and y<m)
        {
                if(dep[i][j]+1<dep[x][y]){
                    dep[x][y]=dep[i][j]+1;
                    bfs(x,y);
                }
        }
    }
}
int main()
{
    fast;
    reset();
    cin>>n>>m>>q;
    int brr[q+1];
    memset(brr,0,sizeof brr);
    FOR(i,n)
    {
        FOR(j,m) cin>>grid[i][j];
    }
    FOR(i,n)
    {
        FOR(j,m)
        {
            for(ll k=0; k<4; k++)
            {
                ll x=i+dx[k];
                ll y=j+dy[k];
                if(x>=0 and x<n and y>=0 and y<m and grid[i][j]==grid[x][y])
                {
                    dep[i][j]=0;
                    break;
                }
            }
        }
    }
    FOR(i,n)
    {
        FOR(j,m)
        {
            if(dep[i][j]==0) bfs(i,j);
        }
    }
    FOR(i,q)
    {
        ll idx;
        cin>>x>>y>>idx;
        --x;
        --y;
        ll ok=dep[x][y];
        ///cout<<x<<" "<<y<<" "<<ok<<endl;
        if(ok>=idx)
        {
            cout<<grid[x][y]<<endl;
        }
        else{
            ll rem=idx-ok;
            ///cout<<rem<<" "<<grid[x][y]<<endl;
            if(rem&1) cout<<alter(grid[x][y])<<endl;
            else cout<<grid[x][y]<<endl;
        }
    }
    return 0;
}


/*Ankon Ghosh
  KUET,ECE 2K16*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
///#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define inf 1e18
#define MAX 1002
#define mod 1000000007
#define sf(a) scanf("%lld",&a)
#define sff(a,b) scanf("%lld %lld",&a,&b)
#define sfff(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define Case(i) printf("Case %lld: ",i)
#define setpre(number) cout<<fixed<<setprecision(10)<<number<<endl
#define pf(a) printf("%lld ",a)
#define pff(a,b) printf("%lld %lld\n",a,b)
#define FOR(i,n) for(ll i=0;i<n;i++)
#define FOR1(i,n) for(ll i=1;i<=n;i++)
#define LOOP(x,y) for(ll i=x;i<=y;i++)
#define RFOR(i,n) for(ll i=n-1;i>=0;i--)
#define RFOR1(i,n) for(ll i=n;i>0;i--)
#define RLOOP(x,y) for(ll i=y;i>=x;i--)
#define m_p make_pair
#define ff first
#define ss second
#define p_b push_back
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)
map<ll,ll>mp1;
vector<pair<ll,pair<ll,ll> > >vpp1;
vector<pair<ll,ll> >vp,vp1;
vector<ll>v,v1,v2,fin;
map<pair<ll,ll>,bool>mbb;
map<ll,bool>mb;
///ll n,a,b,c,d,e,f,ta,sum=0,cnt=0,m,l,req,sz,maxi,k;
bool sortbysec(const pair<pair<ll,ll>,ll> &a,const pair<pair<ll,ll>,ll> &b)
{
    return (a.second < b.second);
}
char grid[MAX+2][MAX+2];
ll dep[MAX+2][MAX+2];
ll n,m,q;
ll x,y,idx;
ll dx[]= {1,0,0,-1};
ll dy[]= {0,1,-1,0};
bool flag=0;
vector<pair<ll,pair<ll,pair<ll,ll> > > >arr;
ll cnt=0;
char alter(char ch)
{
    if(ch=='0')
        return '1';
    else
        return '0';
}
void reset()
{
    FOR(i,MAX)
    {
        FOR(j,MAX) dep[i][j]=inf;
    }
}
void bfs(ll i,ll j,ll cnt)
{
    ///cout<<i<<" "<<j<<endl;
    char ch=grid[i][j];
    for(ll k=0; k<4; k++)
    {
        ll x=i+dx[k];
        ll y=j+dy[k];
        if(x>=0 and x<n and y>=0 and y<m)
        {
            char ch1=grid[x][y];
            //if(ch1!=ch)
           // {
                if(dep[i][j]+1<=dep[x][y])
                bfs(x,y,dep[i][j]+1);
                dep[x][y]=min(dep[x][y],dep[i][j]+1);
            //}
        }
    }
}
int main()
{
    fast;
    reset();
    cin>>n>>m>>q;
    int brr[q+1];
    memset(brr,0,sizeof brr);
    FOR(i,n)
    {
        FOR(j,m) cin>>grid[i][j];
    }
    FOR(i,n)
    {
        FOR(j,m)
        {
            for(ll k=0; k<4; k++)
            {
                ll x=i+dx[k];
                ll y=j+dy[k];
                if(x>=0 and x<n and y>=0 and y<m and grid[i][j]==grid[x][y])
                {
                    dep[i][j]=0;
                    break;
                }
            }
        }
    }
    FOR(i,n)
    {
        FOR(j,m)
        {
            if(dep[i][j]==0) bfs(i,j,0LL);
        }
    }
    FOR(i,q)
    {
        ll idx;
        cin>>x>>y>>idx;
        --x;
        --y;
        ll ok=dep[x][y];
        ///cout<<x<<" "<<y<<" "<<ok<<endl;
        if(ok>=idx)
        {
            cout<<grid[x][y]<<endl;
        }
        else{
            ll rem=idx-ok;
            ///cout<<rem<<" "<<grid[x][y]<<endl;
            if(rem&1) cout<<alter(grid[x][y])<<endl;
            else cout<<grid[x][y]<<endl;
        }
    }
    return 0;
}


/*Ankon Ghosh
  KUET,ECE 2K16*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
///#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define inf 1e18
#define MAX 1002
#define mod 1000000007
#define sf(a) scanf("%lld",&a)
#define sff(a,b) scanf("%lld %lld",&a,&b)
#define sfff(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define Case(i) printf("Case %lld: ",i)
#define setpre(number) cout<<fixed<<setprecision(10)<<number<<endl
#define pf(a) printf("%lld ",a)
#define pff(a,b) printf("%lld %lld\n",a,b)
#define FOR(i,n) for(ll i=0;i<n;i++)
#define FOR1(i,n) for(ll i=1;i<=n;i++)
#define LOOP(x,y) for(ll i=x;i<=y;i++)
#define RFOR(i,n) for(ll i=n-1;i>=0;i--)
#define RFOR1(i,n) for(ll i=n;i>0;i--)
#define RLOOP(x,y) for(ll i=y;i>=x;i--)
#define m_p make_pair
#define ff first
#define ss second
#define p_b push_back
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)
map<ll,ll>mp1;
vector<pair<ll,pair<ll,ll> > >vpp1;
vector<pair<ll,ll> >vp,vp1;
vector<ll>v,v1,v2,fin;
map<pair<ll,ll>,bool>mbb;
map<ll,bool>mb;
///ll n,a,b,c,d,e,f,ta,sum=0,cnt=0,m,l,req,sz,maxi,k;
bool sortbysec(const pair<pair<ll,ll>,ll> &a,const pair<pair<ll,ll>,ll> &b)
{
    return (a.second < b.second);
}
char grid[MAX+2][MAX+2],grid2[MAX+2][MAX+2];
int visited[MAX+2][MAX+2];
int n,m,q;
ll x,y,idx;
int dx[]= {1,0,0,-1};
int dy[]= {0,1,-1,0};
bool flag=0;
vector<pair<ll,pair<ll,pair<ll,ll> > > >arr;
int cnt=0;
char alter(char ch)
{
    if(ch=='0')
        return '1';
    else
        return '0';
}
void bfs(int i,int j,int cnt)
{
    ///cout<<i<<" "<<j<<endl;
    char ch=grid[i][j];
    visited[i][j]=cnt;
    for(ll k=0; k<4; k++)
    {
        int x=i+dx[k];
        int y=j+dy[k];
        if(x>=0 and x<n and y>=0 and y<m and visited[x][y]!=cnt and grid[x][y]==ch)
        {
            flag=1;
            bfs(x,y,cnt);
        }
    }
    if(flag)
    {
        ch=alter(ch);
        grid2[i][j]=ch;
        ///if(grid2[i][j]=='0')
        /// cnt++;
    }
    else{
        grid2[i][j]=grid[i][j];
    }
}
int main()
{
    fast;
    memset(visited,0,sizeof visited);
    cin>>n>>m>>q;
    int brr[q+1];
    memset(brr,0,sizeof brr);
    FOR(i,n)
    {
        FOR(j,m) cin>>grid[i][j];
    }
    FOR(i,q)
    {
        ll idx;
        cin>>x>>y>>idx;
        if(n==1 and m==1)
        {
            cout<<grid[0][0]<<endl;
        }
        arr.p_b(m_p(idx,m_p(x,m_p(y,i))));
    }
    if(n==1 and m==1) return 0;
    bool done1=0;
    bool done2=0;
    int itr=0;
    bool zero=0,one=0;
    ll koto=1;
    sort(arr.begin(),arr.end());
    /*FOR(i,q)
    {
        cout<<arr[i].ff<<" "<<arr[i].ss.ff<<" "<<arr[i].ss.ss<<endl;
    }*/
    FOR(i,q)
    {
        idx=arr[i].ff;
        x=arr[i].ss.ff;
        y=arr[i].ss.ss.ff;
        ll b=arr[i].ss.ss.ss;
        ///cout<<itr<<" "<<done1<<" "<<done2<<" "<<idx<<endl;
        while(itr<idx and done1==false and done2==false)
        {
            flag=0;
            bool flag2=0;
            cnt=0;
            FOR(j,n)
            {
                FOR(k,m)
                {
                    if(visited[j][k]!=koto){
                        flag=0;
                        bfs(j,k,koto);
                        if(flag) flag2=1;
                    }
                }
            }
            /*cout<<"itr: "<<itr<<endl;
            FOR(j,n)
            {
                FOR(k,m) cout<<grid2[j][k];
                cout<<endl;
            }*/
            FOR(j,n)
            {
                ///FOR(k,m) cout<<grid2[j][k];
                FOR(k,m)
                {
                    grid[j][k]=grid2[j][k];
                    if(grid[j][k]=='0')
                        cnt++;

                    ///cout<<grid[j][k];
                }
               ///cout<<endl;
            }
            ///cout<<endl;
            koto++;
            if(cnt==0 or cnt==n*n)
            {
                done1=1;
                if(cnt==n*n)
                    zero=1;
                else
                    one=1;
                itr++;
                break;
            }
            else if(flag2==0)
            {
                done2=1;
                itr++;
                break;
            }
            ///cout<<done1<<" "<<done2<<endl;
            itr++;
        }
        ///cout<<done1<<" "<<done2<<endl;
        if(!done1 and !done2)
        {
            brr[b]=grid[x-1][y-1]-48;
            ///cout<<grid[x-1][y-1]<<endl;
            continue;
        }
        else if(done1 and one)
        {
            ll rem=idx-itr;
            if(rem&2)
                brr[b]=0;
            else
                brr[b]=1;

            continue;
        }
        else if(done1 and zero)
        {
            ll rem=idx-itr;
            if(rem&2)
                brr[b]=1;
            else
                brr[b]=0;
            continue;
        }
        else if(done2)
        {
            brr[b]=grid[x-1][y-1]-48;
        }
    }
    FOR(i,q) cout<<brr[i]<<endl;
    return 0;
}


/*Ankon Ghosh
  KUET,ECE 2K16*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
///#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define inf 1e18
#define MAX 1002
#define mod 1000000007
#define sf(a) scanf("%lld",&a)
#define sff(a,b) scanf("%lld %lld",&a,&b)
#define sfff(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define Case(i) printf("Case %lld: ",i)
#define setpre(number) cout<<fixed<<setprecision(10)<<number<<endl
#define pf(a) printf("%lld\n",a)
#define pff(a,b) printf("%lld %lld\n",a,b)
#define FOR(i,n) for(ll i=0;i<n;i++)
#define FOR1(i,n) for(ll i=1;i<=n;i++)
#define LOOP(x,y) for(ll i=x;i<=y;i++)
#define RFOR(i,n) for(ll i=n-1;i>=0;i--)
#define RFOR1(i,n) for(ll i=n;i>0;i--)
#define RLOOP(x,y) for(ll i=y;i>=x;i--)
#define m_p make_pair
#define ff first
#define ss second
#define p_b push_back
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)
map<ll,ll>mp1;
vector<pair<ll,pair<ll,ll> > >vpp1;
vector<pair<ll,ll> >vp,vp1;
vector<ll>v,v1,v2,fin;
map<pair<ll,ll>,bool>mbb;
map<ll,bool>mb;
///ll n,a,b,c,d,e,f,ta,sum=0,cnt=0,m,l,req,sz,maxi,k;
bool sortbysec(const pair<pair<ll,ll>,ll> &a,const pair<pair<ll,ll>,ll> &b)
{
    return (a.second < b.second);
}
ll grid[MAX+2][MAX+2];
ll dep[MAX+2][MAX+2];
ll n,m,q;
ll x,y,idx;
ll dx[]= {1,0,0,-1};
ll dy[]= {0,1,-1,0};
bool flag=0;
ll cnt=0;
ll alter(ll ch)
{
    if(ch==0)
        return 1;
    else
        return 0;
}
void reset()
{
    FOR(i,MAX)
    {
        FOR(j,MAX) dep[i][j]=inf;
    }
}
void bfs(ll i,ll j)
{
    for(ll k=0; k<4; k++)
    {
        ll x=i+dx[k];
        ll y=j+dy[k];
        if(x>=0 and x<n and y>=0 and y<m)
        {
            if(dep[i][j]+1<dep[x][y])
            {
                dep[x][y]=dep[i][j]+1;
                bfs(x,y);
            }
        }
    }
}
/*void bfs(ll i,ll j)
{
    queue<pair<ll,ll> >q;
    q.push(m_p(i,j));
    while(!q.empty())
    {
        ll i=q.front().ff;
        ll j=q.front().ss;
        q.pop();
        for(ll k=0; k<4; k++)
        {
            ll x=i+dx[k];
            ll y=j+dy[k];
            if(x>=0 and x<n and y>=0 and y<m)
            {
                if(dep[i][j]+1<dep[x][y])
                {
                    dep[x][y]=dep[i][j]+1;
                    q.push(m_p(x,y));
                }
            }
        }
    }
}*/
int main()
{
    reset();
    ///cin>>n>>m>>q;
    sfff(n,m,q);
    FOR(i,n)
    {
        FOR(j,m)
        {
            char ch;
            scanf(" %c",&ch);
            if(ch=='1')
                grid[i][j]=1;
            else
                grid[i][j]=0;
        }
    }
    FOR(i,n)
    {
        FOR(j,m)
        {
            for(ll k=0; k<4; k++)
            {
                ll x=i+dx[k];
                ll y=j+dy[k];
                if(x>=0 and x<n and y>=0 and y<m and grid[i][j]==grid[x][y])
                {
                    dep[i][j]=0;
                    break;
                }
            }
        }
    }
    ll reff;
    FOR(i,n)
    {
        FOR(j,m)
        {
            if(dep[i][j]==0)
                bfs(i,j);
        }
    }
    /*cout<<"check"<<endl;
    FOR(i,n)
    {
        FOR(j,m) cout<<dep[i][j];
        cout<<endl;
    }
    cout<<"done"<<endl;*/
    FOR(i,q)
    {
        ll idx;
        ///cin>>x>>y>>idx;
        sfff(x,y,idx);
        --x;
        --y;
        ll ok=dep[x][y];
        ///cout<<x<<" "<<y<<" "<<ok<<endl;
        if(ok>=idx)
        {
            pf(grid[x][y]);
            ///cout<<grid[x][y]<<endl;
        }
        else
        {
            ll rem=idx-ok;
            ///cout<<rem<<" "<<grid[x][y]<<endl;
            if(rem&1)
                pf(alter(grid[x][y]));
            else
                pf(grid[x][y]);
        }
    }
    return 0;
}


/*Ankon Ghosh
  KUET,ECE 2K16*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
///#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define inf 1e18
#define MAX 1002
#define mod 1000000007
#define sf(a) scanf("%lld",&a)
#define sff(a,b) scanf("%lld %lld",&a,&b)
#define sfff(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define Case(i) printf("Case %lld: ",i)
#define setpre(number) cout<<fixed<<setprecision(10)<<number<<endl
#define pf(a) printf("%lld\n",a)
#define pff(a,b) printf("%lld %lld\n",a,b)
#define FOR(i,n) for(ll i=0;i<n;i++)
#define FOR1(i,n) for(ll i=1;i<=n;i++)
#define LOOP(x,y) for(ll i=x;i<=y;i++)
#define RFOR(i,n) for(ll i=n-1;i>=0;i--)
#define RFOR1(i,n) for(ll i=n;i>0;i--)
#define RLOOP(x,y) for(ll i=y;i>=x;i--)
#define m_p make_pair
#define ff first
#define ss second
#define p_b push_back
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)
map<ll,ll>mp1;
vector<pair<ll,pair<ll,ll> > >vpp1;
vector<pair<ll,ll> >vp,vp1;
vector<ll>v,v1,v2,fin;
map<pair<ll,ll>,bool>mbb;
map<ll,bool>mb;
///ll n,a,b,c,d,e,f,ta,sum=0,cnt=0,m,l,req,sz,maxi,k;
bool sortbysec(const pair<pair<ll,ll>,ll> &a,const pair<pair<ll,ll>,ll> &b)
{
    return (a.second < b.second);
}
ll grid[MAX+2][MAX+2];
ll dep[MAX+2][MAX+2];
ll n,m,q;
ll x,y,idx;
ll dx[]= {1,0,0,-1};
ll dy[]= {0,1,-1,0};
bool flag=0;
ll cnt=0;
ll alter(ll ch)
{
    if(ch==0)
        return 1;
    else
        return 0;
}
void reset()
{
    FOR(i,MAX)
    {
        FOR(j,MAX) dep[i][j]=inf;
    }
}
void bfs(ll i,ll j)
{
    for(ll k=0; k<4; k++)
    {
        ll x=i+dx[k];
        ll y=j+dy[k];
        if(x>=0 and x<n and y>=0 and y<m)
        {
            if(dep[i][j]+1<dep[x][y])
            {
                dep[x][y]=dep[i][j]+1;
                bfs(x,y);
            }
        }
    }
}
int main()
{
    reset();
    ///cin>>n>>m>>q;
    sfff(n,m,q);
    FOR(i,n)
    {
        FOR(j,m)
        {
            char ch;
            scanf(" %c",&ch);
            if(ch=='1') grid[i][j]=1;
            else grid[i][j]=0;
        }
    }
    FOR(i,n)
    {
        FOR(j,m)
        {
            for(ll k=0; k<4; k++)
            {
                ll x=i+dx[k];
                ll y=j+dy[k];
                if(x>=0 and x<n and y>=0 and y<m and grid[i][j]==grid[x][y])
                {
                    dep[i][j]=0;
                    break;
                }
            }
        }
    }
    FOR(i,n)
    {
        FOR(j,m)
        {
            if(dep[i][j]==0)
                bfs(i,j);
        }
    }
    /*cout<<"check"<<endl;
    FOR(i,n)
    {
        FOR(j,m) cout<<dep[i][j];
        cout<<endl;
    }
    cout<<"done"<<endl;*/
    FOR(i,q)
    {
        ll idx;
        ///cin>>x>>y>>idx;
        sfff(x,y,idx);
        --x;
        --y;
        ll ok=dep[x][y];
        ///cout<<x<<" "<<y<<" "<<ok<<endl;
        if(ok>=idx)
        {
            pf(grid[x][y]);
            ///cout<<grid[x][y]<<endl;
        }
        else
        {
            ll rem=idx-ok;
            ///cout<<rem<<" "<<grid[x][y]<<endl;
            if(rem&1)
                pf(alter(grid[x][y]));
            else
                pf(grid[x][y]);
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long int
using namespace std;

main(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
vector<vector<int>> dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    int n,m,t;
    cin>>n>>m>>t;
    vector<vector<int>>v(n+2,vector<int>(m+2,1e18+1)),vv(n+2,vector<int>(m+2,-1));
    vector<vector<int>> vis(n+1,vector<int> (m+1,0));

    for(int i=1;i<n+1;i++){
      string s;cin>>s;
      for(int j=1;j<m+1;j++){
            vv[i][j]=s[j-1]-'0';
      }
    }
    int c=0;int pk=0;
    int ik=-1,jk=-1;
     for(int i=1;i<n+1;i++){
      for(int j=1;j<m+1;j++){
        if(vv[i][j]!=-1){
        if(vv[i][j]==vv[i-1][j]){ if(pk==0){ik=i;jk=j;pk++;}v[i][j]=0;v[i-1][j]=0;}
        if(vv[i][j]==vv[i][j-1]){if(pk==0){ik=i;jk=j;pk++;}v[i][j]=0;v[i][j-1]=0;}
        if(vv[i][j]==vv[i+1][j]){if(pk==0){ik=i;jk=j;pk++;}v[i][j]=0;v[i+1][j]=0;}
        if(vv[i][j]==vv[i][j+1]){if(pk==0){ik=i;jk=j;pk++;}v[i][j]=0;v[i][j+1]=0;}
        }
      }
     }
     //  for(int i=1;i<=n;i++){
     // for(int j=1;j<=m;j++)
     //  cout<<v[i][j]<<" ";
     // cout<<endl;
     //}

     if(ik==-1){
      while(t--){
        int i,j,p;
        cin>>i>>j>>p;
        cout<<vv[i][j]<<endl;
      }
      return 0;
     }
     queue<pair<int,int>>q;
     q.push({ik,jk});
        while(!q.empty()){
        pair<int, int> p = q.front();
        q.pop();
        for(int k=0;k<4;k++){
            int xx = p.first + dir[k][0];
            int yy = p.second + dir[k][1];
            if(xx>0&&xx<=n&&yy>0&&yy<=m&&vis[xx][yy]==0){
                vis[xx][yy] = 1;
                v[xx][yy] = min(1 + v[p.first][p.second],v[xx][yy]);
                q.push({xx, yy});
            }
        }

    }
     //cout<<ik<<" "<<jk<<endl;
     //cout<<endl<<endl;
     //  for(int i=1;i<=n;i++){
      //for(int j=1;j<=m;j++)
      //  cout<<v[i][j]<<" ";
      //cout<<endl;
     //}
     while(t--){
      int i,j,p;
      cin>>i>>j>>p;
      int kc=v[i][j];
      if(kc>=p){cout<<vv[i][j]<<endl;}
      else{
        kc-=p;
        if(kc%2==0)cout<<vv[i][j]<<endl;
        else cout<<1-vv[i][j]<<endl;
      }
     }
}


#include<bits/stdc++.h>
#define int long long int
using namespace std;

main(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int n,m,t;
    cin>>n>>m>>t;
    vector<vector<int>>v(n+2,vector<int>(m+2,1e18+1)),vv(n+2,vector<int>(m+2,-1));
    map<pair<int,int>,int>vis;
    for(int i=1;i<n+1;i++){
      string s;cin>>s;
      for(int j=1;j<m+1;j++){
            vv[i][j]=s[j-1]-'0';
      }
    }
    int c=0;
    int ik=-1,jk=-1;
     for(int i=1;i<n+1;i++){
      for(int j=1;j<m+1;j++){
        if(vv[i][j]!=-1){
        if(vv[i][j]==vv[i-1][j]){ik=i;jk=j;v[i][j]=0;v[i-1][j]=0;}
        if(vv[i][j]==vv[i][j-1]){ik=i;jk=j;v[i][j]=0;v[i][j-1]=0;}
        if(vv[i][j]==vv[i+1][j]){ik=i;jk=j;v[i][j]=0;v[i+1][j]=0;}
        if(vv[i][j]==vv[i][j+1]){ik=i;jk=j;v[i][j]=0;v[i][j+1]=0;}
        }
      }
     }
     if(ik==-1){
      while(t--){
        int i,j,p;
        cin>>i>>j>>p;
        cout<<vv[i][j]<<endl;
      }
      return 0;
     }
     queue<pair<int,int>>q;
     q.push({ik,jk});
     while(!q.empty()){
      pair<int,int> top=q.front();
      q.pop();
        int i=top.first;
        int j=top.second;
        vector<pair<int,int>>temp;
        temp.push_back({i,j-1});
        temp.push_back({i,j+1});
       temp.push_back({i+1,j});
       temp.push_back({i-1,j});
        for(auto cc:temp){
          if(vis[{cc.first,cc.second}]>0){
            v[cc.first][cc.second]=min( v[cc.first][cc.second],1+v[i][j]);
          }
          else{
            vis[{cc.first,cc.second}]++;
             v[cc.first][cc.second]= v[cc.first][cc.second],1+v[i][j];
             if(cc.first<=n && cc.first>=1 && cc.second>=1 && cc.second<=m)q.push({cc.first,cc.second});
          }
        }
     }
     while(t--){
      int i,j,p;
      cin>>i>>j>>p;
      int kc=v[i][j];
      if(kc>=p){cout<<vv[i][j]<<endl;}
      else{
        kc-=p;
        if(kc%2==0)cout<<vv[i][j]<<endl;
        else cout<<1-vv[i][j]<<endl;
      }
     }
}