/* paiza POH! vol.2
* result:
* http://p...content-available-to-author-only...a.jp/poh/paizen/result/0419cbb09ca38d2f33c8c962cdf39a15
* author: Leonardone @ NEETSDKASU
*/
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
static int[][] space2left = new int[300][300];
static int[][] space2top = new int[300][300];
static int[][] result = new int[301][301];
static int[] space2leftCount = new int[301];
static int[] space2topCount = new int[301];
static int[] startPointX = new int[30300];
static int[] startPointY = new int[30300];
{
int[][] space2left = Main.space2left;
int[][] space2top = Main.space2top;
int[][] result = Main.result;
int[] space2leftCount = Main.space2leftCount;
int[] space2topCount = Main.space2topCount;
int[] startPointX = Main.startPointX;
int[] startPointY = Main.startPointY;
final String NEWLINE
= System.
getProperty("line.separator");
int x, y, i, j, ty, dx, dy, s, t;
String[] hw
= in.
readLine().
split(" ",
2); final int H
= Integer.
parseInt(hw
[0]); final int W
= Integer.
parseInt(hw
[1]);
int allSpaceCount = 0;
int startPointCount = 0;
int maxSpace2leftLen = 0;
int maxSpace2topLen = 0;
int[] tempSP2Ty1 = null, tempSP2Ty;
int spaceCount, tempCount, count, seekLength;
for (y = 0; y < H; y++)
{
tempSP2Ty = space2top[y];
line = in.readLine();
spaceCount = 0;
for (x = 0; x < W; x++)
{
if (line.charAt(x) == '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 // if (y == 0)
{
tempSP2Ty[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[tempSP2Ty1[x]]++;
if (tempSP2Ty1[x] > maxSpace2topLen)
{
maxSpace2topLen = tempSP2Ty1[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;
}
}
tempSP2Ty1 = tempSP2Ty;
} // for(y)
final int N
= Integer.
parseInt(in.
readLine());
StringBuilder output = new StringBuilder(N * (6 + NEWLINE.length()));
for (i = 0; i < N; i++)
{
st = in.readLine().split(" ", 2);
count = 0;
if (s <= H && t <= W)
{
if (result[s][t] > 0)
{
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--;
} // for(dy)
if (dy == s)
{
count++;
}
}
x--;
} // for(dx)
} // for(j)
}
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 // 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