/* paiza POH! vol.2
* result:
* http://p...content-available-to-author-only...a.jp/poh/paizen/result/2205173e25de67eb8b181dc573a577f9
* author: Leonardone @ NEETSDKASU
*/
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
{
String[] hw
= in.
readLine().
split(" ",
2); final int H
= Integer.
parseInt(hw
[0]); final int W
= Integer.
parseInt(hw
[1]); final String NEWLINE
= System.
getProperty("line.separator");
int[][] space2left = new int[H][W];
int[][] space2top = new int[H][W];
int allSpaceCount = 0;
int[] startPointX = new int[H * (1 + W / 3)];
int[] startPointY = new int[H * (1 + W / 3)];
int startPointCount = 0;
int[] space2leftCount = new int[W + 1];
int[] space2topCount = new int[H + 1];
int maxSpace2leftLen = 0;
int maxSpace2topLen = 0;
for (int y = 0; y < H; y++)
{
int spaceCount = 0;
for (int x = 0; x < W; x++)
{
char ch = line.charAt(x);
if (ch == '0') // 空
{
spaceCount++;
allSpaceCount++;
if (y > 0)
{
space2top[y][x] = space2top[y - 1][x] + 1;
if (y == H - 1 && space2top[y][x] > 1)
{
space2topCount[space2top[y][x]]++;
if (space2top[y][x] > maxSpace2topLen)
{
maxSpace2topLen = space2top[y][x];
}
}
}
else // if (y == 0)
{
space2top[y][x] = 1;
}
}
else // if (ch == '1') // 埋
{
if (spaceCount > 1)
{
startPointX[startPointCount] = x - 1;
startPointY[startPointCount] = y;
startPointCount++;
space2leftCount[spaceCount]++;
if (spaceCount > maxSpace2leftLen)
{
maxSpace2leftLen = spaceCount;
}
}
spaceCount = 0;
// space2top[y][x] = 0; // 初期値のまんま
if (y > 0) {
space2topCount[space2top[y - 1][x]]++;
if (space2top[y - 1][x] > maxSpace2topLen)
{
maxSpace2topLen = space2top[y - 1][x];
}
}
}
space2left[y][x] = spaceCount;
} // for(x)
if (spaceCount > 1)
{
startPointX[startPointCount] = W - 1;
startPointY[startPointCount] = y;
startPointCount++;
space2leftCount[spaceCount]++;
if (spaceCount > maxSpace2leftLen)
{
maxSpace2leftLen = spaceCount;
}
}
} // for(y)
final int N
= Integer.
parseInt(in.
readLine()); int[][] result = new int[301][301];
StringBuilder output = new StringBuilder(N * (1 + NEWLINE.length()));
for (int i = 0; i < N; i++)
{
String[] st
= in.
readLine().
split(" ",
2);
int count = 0;
if (s <= H && t <= W)
{
if (result[s][t] > 0)
{
count = result[s][t] - 1;
}
else if (s != 1 && t != 1)
{
for (int j = 0; j < startPointCount; j++)
{
int x = startPointX[j];
int y = startPointY[j];
int seekLength = space2left[y][x] - t + 1;
for (int dx = 0; dx < seekLength; dx++)
{
int tx = x - dx;
if (space2top[y][tx] < s)
{
continue;
}
int dy;
for (dy = 0; dy < s; dy++)
{
if (space2left[y - dy][tx] < t)
{
break;
}
} // for(dy)
if (dy == s)
{
count++;
}
} // for(dx)
} // for(j)
}
else if (s != 1)
{
for (int j = s; j <= maxSpace2topLen; j++)
{
count += space2topCount[j] * (j - s + 1);
}
}
else if (t != 1)
{
for (int j = t; j <= maxSpace2leftLen; j++)
{
count += space2leftCount[j] * (j - t + 1);
}
}
else // if (s == 1 && t == 1)
{
count = allSpaceCount;
}
}
output.append(count);
output.append(NEWLINE);
result[s][t] = count + 1;
} // for(i)
} // void main([String)
} // class Main