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

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

int spaceCount, count;

int main(void) {
	int H, W, N, s, t, i, j, x, y;
	char str[310];
	
	scanf("%d %d", &H, &W);
	
	for (y = 0; y < H; ++y) {
		scanf("%s", str);
		spaceCount = 0;
		for (x = 0; x < W; ++x) {
			if (str[x] == '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;
			}
		}
	}
	
	scanf("%d", &N);
	
	for (i = 0; i < N; ++i) {
		scanf("%d %d", &s, &t);
		
		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;
			}
		}
		
		printf("%d\n", count);
	}
	
	return 0;
}
