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

class Main
{
	static byte[] buf = new byte[900000];
	static int ptr = 0;
	
	static int getInt() throws java.lang.Exception
	{
		int v = 0;
		char ch;
		do
		{
			ch = (char)buf[ptr++];
		} while (ch < '0' || ch > '9');
		do
		{
			v = 10 * v + (int)(ch - '0');
			ch = (char)buf[ptr++];
		} while (ch >= '0' && ch <= '9');
		return v;
	}
	
	static char getChar()
	{
		char ch;
		do
		{
			ch = (char)buf[ptr++];
		} while (ch < '0' || ch > '9');
		return ch;
	}
	
	public static void main (String[] args) throws java.lang.Exception
	{
		System.in.read(buf, 0, buf.length);
		
		final int H = getInt();
		final int W = getInt();
		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];
		
		for (int y = 0; y < H; y++)
		{
			int spaceCount = 0;
			for (int x = 0; x < W; x++)
			{
				char ch = getChar();
				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]]++;
						}
					}
					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]++;
					}
					spaceCount = 0;
					// space2top[y][x] = 0; // 初期値のまんま
					if (y > 0) {
						space2topCount[space2top[y - 1][x]]++;
					}
				}
				space2left[y][x] = spaceCount;
			} // for(x)
			if (spaceCount > 1)
			{
				startPointX[startPointCount] = W - 1;
				startPointY[startPointCount] = y;
				startPointCount++;
				space2leftCount[spaceCount]++;
			}
		} // for(y)
		
		final int N = getInt();
		int[][] result = new int[301][301];
		
		StringBuilder output = new StringBuilder(N * (1 + NEWLINE.length()));
		
		for (int i = 0; i < N; i++)
		{
			int s = getInt();
			int t = getInt();
			
			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 <= H; j++)
					{
						count += space2topCount[j] * (j - s + 1);
					}
				}
				else if (t != 1)
				{
					for (int j = t; j <= W; 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)
		
		System.out.print(output);
		
	} // void main([String)
	
} // class Main