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

const int Maxn = 2005;

int n, m, q;
char B[Maxn][Maxn];
int sq[Maxn][Maxn];
int edH[Maxn][Maxn], edV[Maxn][Maxn];

int main()
{
    scanf("%d %d %d", &n, &m, &q);
    for (int i = 1; i <= n; i++)
        scanf("%s", B[i] + 1);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            if (i + 1 <= n && B[i][j] == '1' && B[i + 1][j] == '1')
                edH[i + 1][j]++;
            if (j + 1 <= m && B[i][j] == '1' && B[i][j + 1] == '1')
                edV[i][j + 1]++;
        }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            sq[i][j] = sq[i - 1][j] + sq[i][j - 1] - sq[i - 1][j - 1] + int(B[i][j] == '1');
            edH[i][j] += edH[i - 1][j] + edH[i][j - 1] - edH[i - 1][j - 1];
            edV[i][j] += edV[i - 1][j] + edV[i][j - 1] - edV[i - 1][j - 1];
        }
    while (q--) {
        int r1, c1, r2, c2; scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
        int V = sq[r2][c2] - sq[r1 - 1][c2] - sq[r2][c1 - 1] + sq[r1 - 1][c1 - 1];
        int E = edH[r2][c2] - edH[r1][c2] - edH[r2][c1 - 1] + edH[r1][c1 - 1] +
                edV[r2][c2] - edV[r1 - 1][c2] - edV[r2][c1] + edV[r1 - 1][c1];
        printf("%d\n", V - E);
    }
    return 0;
}

#include<cmath>
#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
	register char ch;
	while(!isdigit(ch=getchar()));
	register int x=ch^'0';
	while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
	return x;
}
const int N=600;
int n,d1,d2,col[2][N*N];
std::vector<std::pair<int,int>> v[2];
inline int id(const int &x,const int &y) {
	return x*N+y;
}
inline bool check(const int &x) {
	return 0<=x&&x<n*2;
}
void dfs(const int &x,const int &t) {
	const int i=x/N,j=x%N;
	for(auto &d:v[t]) {
		const int nx=i+d.first,ny=j+d.second;
		if(!check(nx)||!check(ny)) continue;
		const int y=id(i+d.first,j+d.second);
		if(!col[t][y]) {
			col[t][y]=col[t][x]==1?2:1;
			dfs(y,t);
		}
	}
}
int main() {
	n=getint(),d1=getint(),d2=getint();
	for(register int i=0;i<=d1;i++) {
		const int j=sqrt(d1-i*i);
		if(i*i+j*j!=d1) continue;
		v[0].emplace_back(std::make_pair(i,j));
		v[0].emplace_back(std::make_pair(-i,j));
		v[0].emplace_back(std::make_pair(i,-j));
		v[0].emplace_back(std::make_pair(-i,-j));
	}
	for(register int i=0;i<=d2;i++) {
		const int j=sqrt(d2-i*i);
		if(i*i+j*j!=d2) continue;
		v[1].emplace_back(std::make_pair(i,j));
		v[1].emplace_back(std::make_pair(-i,j));
		v[1].emplace_back(std::make_pair(i,-j));
		v[1].emplace_back(std::make_pair(-i,-j));
	}
	for(register int i=0;i<n*2;i++) {
		for(register int j=0;j<n*2;j++) {
			if(!col[0][id(i,j)]) {
				col[0][id(i,j)]=1;
				dfs(id(i,j),0);
			}
			if(!col[1][id(i,j)]) {
				col[1][id(i,j)]=1;
				dfs(id(i,j),1);
			}
		}
	}
	for(register int i=0,cnt=0;i<n*2;i++) {
		for(register int j=0;j<n*2;j++) {
			if(col[0][id(i,j)]==1&&col[1][id(i,j)]==1) {
				printf("%d %d\n",i,j);
				if(++cnt==n*n) return 0;
			}
		}
	}
}