/* paiza POH! vol.2
 * result:
 * http://p...content-available-to-author-only...a.jp/poh/paizen/result/720571daa885443d3cfd98d72fec1d3b
 * author: Leonardone @ NEETSDKASU
 */
#include <stdio.h>

#define OUTPUTSIZE (600000)
char output[OUTPUTSIZE];
char *optr = output;

void putInt(int v) {
	if (v < 10) {
		*optr = '0' + (char)v;
		++optr;
	} else {
		putInt(v / 10);
		*optr = '0' + (char)(v % 10);
		++optr;
	}
}

void putNewline(void) {
	*optr = '\n';
	++optr;
}

#define BUFSIZE (900000)
char buf[BUFSIZE];
char *ptr = buf;

int getInt(void) {
	int v = 0;
	while (*ptr < '0' || *ptr > '9') ++ptr;
	while (*ptr >= '0' && *ptr <= '9') {
		v = 10 * v + (int)(*ptr - '0');
		++ptr;
	}
	return v;
}

char getChar(void) {
	while (*ptr < '0' || *ptr > '9') ++ptr;
	return *ptr++;
}

int space2left[300][300];
int table[301][301];
int cache[301][301];

int spaceCount, tempCount, count;

int *tempSP2Ty;
int *tempSP2Ty1;

int main(void) {
	int H, W, N, s, t, i, j, x, y, dx, ty, dy;
	
	fread(buf, sizeof(char), BUFSIZE, stdin);
	
	H = getInt();
	W = getInt();
	
	for (y = 0; y < H; ++y) {
		spaceCount = 0;
		for (x = 0; x < W; ++x) {
			if (getChar() == '0') {
				++spaceCount;
			} else {
				spaceCount = 0;
			}
			space2left[y][x] = spaceCount;
		}
	}
	
	for (y = 0; y < H; ++y) {
		for (x = W - 1; x >= 0; --x) {
			if (space2left[y][x] == 0) {
				continue;
			}
			s = 1;
			t = space2left[y][x];
			for (i = y; i < H && space2left[i][x] > 0; i++) {
				if (space2left[i][x] < t) {
					t = space2left[i][x];
				}
				++table[s][t];
				++s;
			}
		}
	}
	
	N = getInt();
	
	for (i = 0; i < N; ++i) {
		s = getInt();
		t = getInt();
		count = 0;
		
		if (s <= H && t <= W) {
			if (cache[s][t]) {
				count = cache[s][t] - 1 ;
			} else {
				for (j = t; j <= W; ++j) {
					count += table[s][j];
				}
				cache[s][t] = count + 1;
			}
		}
		
		putInt(count);
		putNewline();
	}
	
	fwrite(output, sizeof(char), (int)optr - (int)output, stdout);
	return 0;
}
