/* paiza POH! vol.2
* result:
* http://p...content-available-to-author-only...a.jp/poh/paizen/result/60c96cab5eb14042b7e8df20c2b99382
* author: Leonardone @ NEETSDKASU
*/
#include <stdio.h>
#define OUTPUTSIZE (600000)
char output[OUTPUTSIZE];
char *optr = output;
void putInt(int v) {
if (v < 10) {
*optr = '0' + (char)v;
++optr;
} else {
putInt(v / 10);
*optr = '0' + (char)(v % 10);
++optr;
}
}
void putNewline(void) {
*optr = '\n';
++optr;
}
#define BUFSIZE (900000)
char buf[BUFSIZE];
char *ptr = buf;
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++;
}
int space2left[300][300];
int space2top[300][300];
int result[301][301];
int space2leftCount[301];
int space2topCount[301];
int startPointX[30300];
int startPointY[30300];
int startPointCount;
int allSpaceCount;
int maxSpace2leftLen;
int maxSpace2topLen;
int seekLength;
int spaceCount, tempCount, count;
int *tempSP2Ty;
int *tempSP2Ty1;
int main(void) {
int H, W, N, s, t, i, j, x, y, dx, ty, dy;
fread(buf
, sizeof(char), BUFSIZE
, stdin
);
H = getInt();
W = getInt();
for (y = 0; y < H; ++y) {
spaceCount = 0;
tempSP2Ty = space2top[y];
for (x = 0; x < W; ++x) {
if (getChar() == '0') {
++spaceCount;
++allSpaceCount;
if (y > 0) {
tempCount = tempSP2Ty[x] = tempSP2Ty1[x] + 1;
if (y == H - 1 && tempCount > 1) {
++space2topCount[tempCount];
if (tempCount > maxSpace2topLen) {
maxSpace2topLen = tempCount;
}
}
} else {
tempSP2Ty[x] = 1;
}
} else {
if (spaceCount > 1) {
startPointX[startPointCount] = x - 1;
startPointY[startPointCount] = y;
++startPointCount;
++space2leftCount[spaceCount];
if (spaceCount > maxSpace2leftLen) {
maxSpace2leftLen = spaceCount;
}
}
spaceCount = 0;
tempSP2Ty[x] = 0;
if (y > 0 && tempSP2Ty1[x] > 1) {
++space2topCount[tempSP2Ty1[x]];
if (tempSP2Ty1[x] > maxSpace2topLen) {
maxSpace2topLen = tempSP2Ty1[x];
}
}
}
space2left[y][x] = spaceCount;
}
if (spaceCount > 1) {
startPointX[startPointCount] = W - 1;
startPointY[startPointCount] = y;
++startPointCount;
++space2leftCount[spaceCount];
if (spaceCount > maxSpace2leftLen) {
maxSpace2leftLen = spaceCount;
}
}
tempSP2Ty1 = tempSP2Ty;
}
N = getInt();
for (i = 0; i < N; ++i) {
s = getInt();
t = getInt();
count = 0;
if (s <= H && t <= W) {
if (result[s][t]) {
count = result[s][t] - 1;
} else {
if (s != 1 && t != 1) {
for (j = 0; j < startPointCount; ++j) {
x = startPointX[j];
y = startPointY[j];
seekLength = space2left[y][x] - t + 1;
tempSP2Ty = space2top[y];
for (dx = 0; dx < seekLength; ++dx) {
if (tempSP2Ty[x] >= s) {
ty = y;
for (dy = 0; dy < s; ++dy) {
if (space2left[ty][x] < t) {
break;
}
--ty;
}
if (dy == s) {
++count;
}
}
--x;
}
}
} else if (s != 1) {
for (j = s; j <= maxSpace2topLen; ++j) {
count += space2topCount[j] * (j - s + 1);
}
} else if (t != 1) {
for (j = t; j <= maxSpace2leftLen; ++j) {
count += space2leftCount[j] * (j - t + 1);
}
} else {
count = allSpaceCount;
}
result[s][t] = count + 1;
}
}
putInt(count);
putNewline();
}
fwrite(output
, sizeof(char), (int)optr
- (int)output
, stdout
); return 0;
}