/* paiza POH! vol.2
 * result: 
 * http://p...content-available-to-author-only...a.jp/poh/paizen/result/52163a4e1d60e34a8e854214187c4699
 * 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(" ");
		final int H = Integer.valueOf(hw[0]); // ホーム画面縦の区画数
		final int W = Integer.valueOf(hw[1]); // ホーム画面横の区画数
		
		int[][] space2left = new int[H][W];
		int[][] space2top = new int[H][W];
		int spacecount = 0;
		
		List<Position> startposition2left = new ArrayList<Position>();
		List<Position> startposition2top = new ArrayList<Position>();
		
		for (int y = 0; y < H; y++)
		{
			String line = in.readLine();
			int space2leftSize = 0;
			for (int x = 0; x < W; x++)
			{
				char ch = line.charAt(x);
				if (ch == '1')
				{
					space2leftSize = 0;
					space2top[y][x] = 0;
					if (x > 0 && space2left[y][x - 1] > 0)
					{
						startposition2left.add(
							new Position(y, x - 1));
					}
					if (y > 0 && space2top[y - 1][x] > 0)
					{
						startposition2top.add(
							new Position(y - 1, x));
					}
				}
				else // if (ch == '0')
				{
					spacecount++;
					space2leftSize++;
					if (y > 0)
					{
						space2top[y][x] = space2top[y - 1][x] + 1;
						if (y == H - 1)
						{
							startposition2top.add(
								new Position(y, x));
						}
					}
					else // if (y == 0)
					{
						space2top[y][x] = 1;
					}
					if (x == W - 1)
					{
						startposition2left.add(
							new Position(y, x));
					}
				}
				space2left[y][x] = space2leftSize;
			}
		}
		// print2dArray(space2left);
		// print2dArray(space2top);
		// System.out.println(startposition2left);
		// System.out.println(startposition2top);
		
		final int N = Integer.valueOf(in.readLine()); // ウィジェット数
		HashSet<Position> placeablePosition = new HashSet<Position>();
		
		for (int i = 0; i < N; i++)
		{
			String[] st = in.readLine().split(" ");
			int s = Integer.valueOf(st[0]); // ウィジェットの縦サイズ
			int t = Integer.valueOf(st[1]); // ウィジェットの横サイズ
			int placeableCount = 0;
			
			if (s == 1) // 横長のウィジェット
			{
				if (t == 1) // 1x1のウィジェット
				{
					placeableCount = spacecount;
				}
				else // if (t != 1) // 横長のウィジェット
				{
					for (Position position : startposition2left)
					{
						int spaceWSize = space2left[position.y][position.x];
						if (spaceWSize >= t)
						{
							placeableCount += spaceWSize - t + 1;
						}
					}
				}
			}
			else if (t == 1) // 縦長のウィジェット
			{
				for (Position position : startposition2top)
				{
					int spaceHSize = space2top[position.y][position.x];
					if (spaceHSize >= s)
					{
						placeableCount += spaceHSize - s + 1;
					}
				}
			}
			else // if (s != 1 && t != 1)
			{
				placeablePosition.clear();
				// 横に走査していく
				for (Position position : startposition2left)
				{
					int spaceWSize = space2left[position.y][position.x];
					for (int dx = 0; dx < spaceWSize; dx++)
					{
						int x = position.x - dx;
						int spaceHSize = space2top[position.y][x];
						if (spaceHSize < s)
						{
							continue; // 縦サイズが足りない
						}
						// 縦に走査して横サイズ以上の連続する空所を数える
						int count = 0;
						for (int dy = 0; dy < spaceHSize; dy++)
						{
							int y = position.y - dy;
							if (space2left[y][x] >= t) 
							{
								count++;
								if (count >= s) // ウィジェットが確保できる
								{
									boolean placed = placeablePosition.add(
														new Position(y, x));
									if (placed == false)
									{
										break; // これ以上の縦の走査は走査済み
									}
								}
							}
							else
							{
								count = 0; // 連続の途切れ
							}
						}
					}
				}
				placeableCount = placeablePosition.size();
			} 
			System.out.println(placeableCount);
		}
		
	} // end of main(String[])
	
	static void print2dArray(int[][] array)
	{
		for (int i = 0; i < array.length; i++)
		{
			for (int j = 0; j < array[i].length; j++)
			{
				System.out.print(array[i][j] + " ");
			}
			System.out.println();
		}
		System.out.println();
	}
	
	static class Position
	{
		final int y;
		final int x;
		Position(int y, int x)
		{
			this.y = y;
			this.x = x;
		}
		
		@Override public String toString()
		{
			return "(" + x + ", " + y + ")";
		}
		
		@Override public int hashCode()
		{
			return y * (x + y);
		}
		
		@Override public boolean equals(Object obj)
		{
			if (this == obj)
			{
				return true;
			}
			if (obj == null)
			{
				return false;
			}
			if (this.getClass().equals(obj.getClass()) == false)
			{
				return false;
			}
			Position pos = (Position)obj;
			return y == pos.y && x == pos.x;
		}
	} // end of class Position
}
