/* paiza POH! vol.2
 * result:
 * http://p...content-available-to-author-only...a.jp/poh/paizen/result/79416a22f9f6ab7b5ed2248a4d7b54e0
 * 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.parseInt(hw[0]); // ホーム画面縦の区画数
		final int W = Integer.parseInt(hw[1]); // ホーム画面横の区画数
		
		int[][] home = new int[H][W];
		int[][] hoge = new int[H][W];
		
		int spacecount = 0;
		
		for (int y = 0; y < H; y++)
		{
			String line = in.readLine();
			int count = 0;
			for (int x = 0; x < W; x++)
			{
				int tx = W - x - 1;
				char ch = line.charAt(tx);
				if (ch == '0')
				{
					spacecount++;
					count++;
					if (y > 0)
					{
						hoge[y][tx] = hoge[y - 1][tx] + 1;
					}
					else
					{
						hoge[y][tx] = hoge[y][tx] = 1;
					}
				}
				else
				{
					count = 0;
					hoge[y][tx] = 0;
				}
				home[y][tx] = count;
			}
		}
		
		final int N = Integer.parseInt(in.readLine()); // ウィジェット数
		
		for (int i = 0; i < N; i++)
		{
			String[] st = in.readLine().split(" ");
			int s = Integer.parseInt(st[0]); // ウィジェットの縦サイズ
			int t = Integer.parseInt(st[1]); // ウィジェットの横サイズ
			
			if (s == 1 && t == 1)
			{
				System.out.println(spacecount);
				continue;
			}
			
			int count = 0;
			for (int hy = H - 1; hy >= s - 1; hy--)
			{
				for (int hx = 0; hx < W - t + 1; hx++)
				{
					if (home[hy][hx] < t)
					{
						hx += home[hy][hx];
						continue;
					}
					if (hoge[hy][hx] < s)
					{
						continue;
					}
					int dy;
					for (dy = 1; dy < s; dy++)
					{
						int y = hy - dy;
						if (y >= H)
						{
							break;
						}
						if (home[y][hx] < t)
						{
							break;
						}
					}
					if (dy == s)
					{
						count++;
					}
				}
			}
			System.out.println(count);
		}
		
	} // end of main(String[])
	
}
