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

char input[100000];
char *ptr = input;

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++;
}

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

int home[300][300];
int hoge[300][300];

int main(void) {
	int H, W, N, s, t;
	int x, y, c, i;
	int hx, hy, hxe, hye;
	int count, dy, dx;
	char ch;
	fread(input, sizeof(char), 100000, stdin);
	H = getInt();
	W = getInt();
	
	for (y = 0; y < H; ++y) {
		c = 0;
		for (x = 0; x < W; ++x) {
			ch = getChar();
			if (ch == '0') {
				c++;
				if (y) {
					hoge[y][x] = hoge[y - 1][x] + 1;
				} else {
					hoge[y][x] = 1;
				}
			} else {
				c = 0;
				hoge[y][x] = 0;
			}
			home[y][x] = c;
		}
	}
	
	N = getInt();
	
	for (i = 0; i < N; ++i) {
		s = getInt();
		t = getInt();
		
		if (s > H || t > W) {
			putchar('0');
			putchar('\n');
			continue;
		}
		
		hye = s - 1;
		hxe = t - 1;
		count = 0;
		for (hy = H - 1; hy >= hye; --hy) {
			for (hx =  W - 1; hx >= hxe; --hx) {
				if (home[hy][hx] < t) {
					hx -= home[hy][hx];
					continue;
				}
				if (hoge[hy][hx] < s) {
					continue;
				}
				for (dy = 1; dy < s; ++dy) {
					y = hy - dy;
					if (y < 0) {
						break;
					}
					if (home[y][hx] < t) {
						break;
					}
				}
				if (dy == s) {
					count++;
				}
			}
		}
		putInt(count);
		putchar('\n');
	}
	
	return 0;
}
