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

final class Main
{
	private static final BufferedReader s_in = new BufferedReader(
			new InputStreamReader(System.in));

	private static final int[][] s_table = new int[301][301];
		
	private static final int[] s_space2top = new int[300];

	private static final int[] s_maxsp = new int[301];

	private static final StringBuilder s_output = new StringBuilder(450000);
		
	
	public static void main (String[] args) throws java.lang.Exception
	{
		final BufferedReader in = s_in;
		final int[][] table = s_table;
		final int[] space2top = s_space2top;
		final StringBuilder output = s_output;
		final int[] maxsp = s_maxsp;
		
		int maxW = 0;
		
		int x, y, i, s, t;
		int[] temp1, temp2;
		String line;
		
		String[] hw = in.readLine().split(" ");
		final int H = Integer.parseInt(hw[0]); // ホーム画面縦の区画数
		final int W = Integer.parseInt(hw[1]); // ホーム画面横の区画数
		

		for (y = 0; y < H; y++) {
			line = in.readLine();
			for (x = 0; x < W; x++) {
				if (line.charAt(x) == '0') {
					space2top[x]++;
					s = space2top[x];
					t = 1;
					for (i = x; i >= 0 && space2top[i] > 0; i--) {
						if (space2top[i] < s) {
							s = space2top[i];
						}
						if (t > maxW) {
							maxW = t;
						}
						if (s > maxsp[t]) {
							maxsp[t] = s;
						}
						table[t][s]++;
						t++;
					}
				} else {
					space2top[x] = 0;
				}
			}
		}
		
		for (t = 1; t <= maxW; t++) {
			temp2 = table[t];
			for (s = maxsp[t] - 1; s > 0; s--) {
				temp2[s] = temp2[s] + temp2[s + 1];
			}
		}
		
		final int N = Integer.parseInt(in.readLine()); // ウィジェット数
		
		for (i = 0; i < N; i++)
		{
			hw = in.readLine().split(" ");
			s = Integer.parseInt(hw[0]); // ウィジェットの縦サイズ
			t = Integer.parseInt(hw[1]); // ウィジェットの横サイズ
			
			output.append(table[t][s]);
			output.append('\n');
		}
		
		System.out.print(output);
		
	} // end of main(String[])
}
