# paiza POH! vol.2
# result:
# http://p...content-available-to-author-only...a.jp/poh/paizen/result/4718ef77205c2f35c3de7437ae514358
# author: Leonardone @ NEETSDKASU
h, w = map(int, raw_input().split())
sp = [[0 for j in range(w)] for i in range(h)]
for y in range(h) :
	str = raw_input()
	c = 0
	for x in range(w) :
		if str[x] == '0' :
			c = c + 1
		else :
			c = 0
		sp[y][x] = c
tb = [[0 for j in range(w + 1)] for i in range(h + 1)]
ca = [[0 for j in range(w + 1)] for i in range(h + 1)]
for y in range(h) :
	for x in range(w) :
		if sp[y][x] == 0 :
			continue
		s, t = 1, sp[y][x]
		for i in range(y, h) :
			if sp[i][x] == 0 :
				break
			t = min(sp[i][x], t)
			tb[s][t] = tb[s][t] + 1
			s = s + 1

n = int(raw_input())
for i in range(n) :
	s, t = map(int, raw_input().split())
	if s > h or t > w :
		print "0"
	else :
		if ca[s][t] > 0 :
			print ca[s][t] - 1
		else :
			c = 0
			for j in range(t, w + 1) :
				c = c + tb[s][j]
			ca[s][t] = c + 1
			print c
	