/* paiza POH! vol.2
* result:
* http://p...content-available-to-author-only...a.jp/poh/paizen/result/720571daa885443d3cfd98d72fec1d3b
* 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 table[301][301];
int cache[301][301];
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;
for (x = 0; x < W; ++x) {
if (getChar() == '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;
}
}
}
N = getInt();
for (i = 0; i < N; ++i) {
s = getInt();
t = getInt();
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;
}
}
putInt(count);
putNewline();
}
fwrite(output
, sizeof(char), (int)optr
- (int)output
, stdout
); return 0;
}
LyogcGFpemEgUE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