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

int space2top[301];
int table[301][301];

typedef int * PINT;

int main(void) {
	int H, W, N, s, t, i, j, x, y;
	char str[310];
	PINT p, q, r;
	
	scanf("%d %d", &H, &W);
	
	r = space2top + 1;
	for (y = 0; y < H; ++y) {
		scanf("%s", str);
		q = r;
		for (x = 0; x < W; ++x, ++q) {
			if (str[x] == '0') {
				p = q;
				s = ++(*p);
				t = 1;
				while (*p) {
					if (*p < s) {
						s = *p;
					}
					++table[t][s];
					++t;
					--p;
				}
			} else {
				*q = 0;
			}
		}
	}
	
	for (x = 1; x <= W; ++x) {
		p = q = &table[x][H];
		for (y = 1; y < H; ++y) {
			--p;
			*p += *q;
			--q;
		}
	}
	
	scanf("%d", &N);
	
	for (i = 0; i < N; ++i) {
		scanf("%d %d", &s, &t);
		printf("%d\n", table[t][s]);
	}
	
	return 0;
}
