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

#define SIZE (9000000)

char input[SIZE];
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[310][310];
int hoge[310][310];
int result[310][310];

int startXm[310];
int startYm[310];

int main(void) {
	int H, W, N, s, t;
	int x, y, c, i, j;
	int hx, hy, hxe, hye;
	int count, dy, dx;
	char ch;
	
	fread(input, sizeof(char), SIZE, 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;
				}
				startXm[c] = x;
				startYm[c] = y;
			} else {
				c = 0;
				hoge[y][x] = 0;
			}
			home[y][x] = c;
		}
	}
	
	N = getInt();
	

	if (W > H) {
		for (i = 0; i < N; ++i) {
			s = getInt();
			t = getInt();
			
			if (s > H || t > W) {
				putchar('0');
				putchar('\n');
				continue;
			}
			
			if (result[s][t]) {
				putInt(result[s][t] - 1);
				putchar('\n');
				continue;
			}
			
			hye = s - 1;
			hxe = t - 1;
			count = 0;

			hx = startXm[t];
			hy = startYm[t];
			for (j = t + 1; j < 301; ++j) {
				if (startYm[j] > hy) {
					hy = startYm[j];
					hx = startXm[j];
				}
			}
			for (; hy >= hye; --hy) {
				for (; 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++;
					}
				}
				hx = W - 1;
			}
			putInt(count);
			putchar('\n');
			result[s][t] = count + 1;
		}
	} else {
		for (i = 0; i < N; ++i) {
			s = getInt();
			t = getInt();
			
			if (s > H || t > W) {
				putchar('0');
				putchar('\n');
				continue;
			}
			
			if (result[s][t]) {
				putInt(result[s][t] - 1);
				putchar('\n');
				continue;
			}
			
			hye = s - 1;
			hxe = t - 1;
			count = 0;
			hx = startXm[t];
			hy = startYm[t];
			for (j = t + 1; j < 301; ++j) {
				if (startXm[j] > hx) {
					hy = startYm[j];
					hx = startXm[j];
				}
			}
			for (; hx >= hxe; --hx) {
				for (; hy >= hye; --hy) {
					if (home[hy][hx] < t) {
						continue;
					}
					if (hoge[hy][hx] < s) {
						hy -= hoge[hy][hx];
						continue;
					}
					for (dy = 1; dy < s; ++dy) {
						y = hy - dy;
						if (y < 0) {
							break;
						}
						if (home[y][hx] < t) {
							break;
						}
					}
					if (dy == s) {
						count++;
					}
				}
				hy = H - 1;
			}
			putInt(count);
			putchar('\n');
			result[s][t] = count + 1;
		}
	}
	
	return 0;
}
