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

class Main
{
	static int[] sp = new int[91000];
	static int[] tb = new int[91000];
	static int[] ca = new int[91000];
	
	public static void main (String[] args) throws java.lang.Exception
	{
		BufferedReader in = new BufferedReader(
			new InputStreamReader(System.in));
		
		String[] hw = in.readLine().split(" ");
		int H = Integer.parseInt(hw[0]); // ホーム画面縦の区画数
		int W = Integer.parseInt(hw[1]); // ホーム画面横の区画数
		
		int[] space2right = sp;
	 	int[] table = tb;
		int[] cache = ca;
		
		int y, x, j ,i, s, t;
		String line;
		int count;
		
		for (y = 0; y < H; y++)
		{
			line = in.readLine();
			count = 0;
			for (x = W - 1; x >= 0; x--)
			{
				if (line.charAt(x) == '0')
				{
					count++;
				}
				else
				{
					count = 0;
				}
				space2right[y * 301 + x] = count;
			}
		}
		
		j = 0;
		for (y = 0; y < H; y++)
		{
			for (x = 0; x < W; x++)
			{
				if (space2right[j + x] == 0)
				{
					continue;
				}
				s = 301;
				t = space2right[j + x];
				for (i = y; i < H && space2right[i * 301 + x] > 0; i++)
				{
					if (space2right[i * 301 + x] < t)
					{
						t = space2right[i * 301 + x];
					}
					table[s + t]++;
					s += 301;
				}
			}
			j += 301;
		}
		
		int N = Integer.parseInt(in.readLine()); // ウィジェット数
		StringBuilder output = new StringBuilder(N * 6);

		for (i = 0; i < N; i++)
		{
			hw = in.readLine().split(" ");
			s = Integer.parseInt(hw[0]); // ウィジェットの縦サイズ
			t = Integer.parseInt(hw[1]); // ウィジェットの横サイズ
			
			count = 0;
			if (s <= H && t <= W)
			{
				y = s * 301;
				x = y + t;
				if (cache[x] > 0)
				{
					count = cache[x] - 1;
				}
				else
				{
					for (j = t; j <= W; j++)
					{
						count += table[y + j];
					}
					cache[x] = count + 1;
				}
			}

			output.append(count);
			output.append('\n');
		}
		
		System.out.print(output);
		
	} // end of main(String[])
	
}
