/* paiza POH! vol.2
 * result:
 * http://p...content-available-to-author-only...a.jp/poh/paizen/result/4b509aa26f3448b1ad4e758f6ea5cbeb
 * author: Leonardone @ NEETSDKASU
 */
import java.util.*;
import java.lang.*;
import java.io.*;

class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		
		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;
		
		int[] tempSP2Ty1 = null;;

		for (int y = 0; y < H; y++)
		{
			int[] tempSP2Ty = space2top[y];
			String line = in.readLine();
			int spaceCount = 0;
			for (int x = 0; x < W; x++)
			{
				if (line.charAt(x) == '0') // 空
				{
					spaceCount++;
					allSpaceCount++;
					if (y > 0)
					{
						int 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());
		int[][] result = new int[301][301];
		
		result[1][1] = allSpaceCount + 1;
		for (int i = 2; i <= maxSpace2topLen; i++) 
		{
			int count = 0;
			for (int j = i; j <= maxSpace2topLen; j++)
			{
				count += space2topCount[j] * (j - i + 1);
			}
			result[i][1] = count + 1;
		}
		for (int i = maxSpace2topLen + 1; i <= H; i++)
		{
			result[i][1] = 1;
		}
		for (int i = 2; i <= maxSpace2leftLen; i++) 
		{
			int count = 0;
			for (int j = i; j <= maxSpace2leftLen; j++)
			{
				count += space2leftCount[j] * (j - i + 1);
			}
			result[1][i] = count + 1;
		}
		if (maxSpace2leftLen < W)
		{
			Arrays.fill(result[1], maxSpace2leftLen + 1, W + 1, 1);
		}
		
		StringBuilder output = new StringBuilder(N * (6 + NEWLINE.length()));
		
		for (int i = 0; i < N; i++)
		{
			String[] st = in.readLine().split(" ", 2);
			int s = Integer.parseInt(st[0]);
			int t = Integer.parseInt(st[1]);
			
			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;
						
						int[] tempSP2Ty = space2top[y];
						for (int dx = 0; dx < seekLength; dx++)
						{
							int tx = x - dx;
							if (tempSP2Ty[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)
				}
			}
			output.append(count);
			output.append(NEWLINE);
			result[s][t] = count + 1;
		} // for(i)
		
		System.out.print(output);
		
	} // void main([String)
	
} // class Main