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

class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Paiza.getInstance().resolve(new MyResolver());
	}
}

class MyResolver extends Paiza.Resolver
{
	int[][][][] table = null;
	int[][] cache = null;
	
	private void debug(String key, String str) {
		System.out.println(key + " :: " + str);
	}
	
	private void printArray(int[][] arr) {
		System.out.println("Array " + arr.length + "x" + arr[0].length);
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j] + " ");
			}
			System.out.println();
		}
		System.out.println("-----");
	}
	
	@Override
	public void setHome(Paiza.Home home) {
		super.setHome(home);
		
		table = new int[301][301][][];
		cache = new int[301][301];
		
		int[][] temp = table[1][1] = new int[home.getH()][home.getW()];
		int count = 0;
		
		for (int y = 0; y < home.getH(); y++) {
			for (int x = 0; x < home.getW(); x++) {
				if (home.isSpace(x, y)) {
					count++;
				} else {
					temp[y][x] = 1;
				}
			}
		}
		
		cache[1][1] = count + 1;
	}
	
	@Override
	public int resolve(Paiza.Widget widget) {
		if (widget.getS() > home.getH() || widget.getT() > home.getW()) {
			return 0;
		}
		if (cache[widget.getS()][widget.getT()] > 0) {
			return cache[widget.getS()][widget.getT()] - 1;
		}
		return tatami(widget);
	}

	private int tatami(Paiza.Widget widget) {
		int tx = 0, ty = 0;
		int x, y;
		int[][] temp1, temp2, temp3;
		
		x = widget.getT();
		for (y = widget.getS(); y > 0; y--) {
			if (table[y][x] != null) {
				tx = x;
				ty = y;
				break;
			}
		}
		y = widget.getS();
		for (x = widget.getT(); x > 0; x--) {
			if (table[y][x] != null) {
				if (x + y > tx + ty) {
					tx = x;
					ty = y;
				}
				break;
			}
		}
		
		if (tx == 0) {
			x = 1;
			for (y = widget.getS(); y > 1; y--) {
				if (table[y][x] != null) {
					tx = x;
					ty = y;
					break;
				}
			}
			y = 1;
			for (x = widget.getT(); x > 1; x--) {
				if (table[y][x] != null) {
					if (x + y > tx + ty) {
						tx = x;
						ty = y;
					}
					break;
				}
			}
			if (tx == 0) {
				tx = ty = 1;
			}
		}

//		debug("tytx", ty + "," + tx);
//		printArray(table[ty][tx]);
		
		if (ty == 1) {
			temp1 = table[1][1];
			temp2 = table[ty][tx];
			for (x = tx + 1; x <= widget.getT(); x++) {
				temp3 = table[ty][x] = new int[temp2.length][temp2[0].length - 1];
				for (int i = 0; i < temp3.length; i++) {
					for (int j = 0; j < temp3[0].length; j++) {
						temp3[i][j] = temp2[i][j] + temp1[i][j + x - 1];
					}
				}
//				debug("make1", ty + "," + x);
//				printArray(temp3);
				temp2 = temp3;
			}
			tx = widget.getT();
		} else if (tx == 1) {
			temp1 = table[1][1];
			temp2 = table[ty][tx];
			for (y = ty + 1; y <= widget.getS(); y++) {
				temp3 = table[y][tx] = new int[temp2.length - 1][temp2[0].length];
				for (int i = 0; i < temp3.length; i++) {
					for (int j = 0; j < temp3[0].length; j++) {
						temp3[i][j] = temp2[i][j] + temp1[i + y - 1][j];
					}
				}
//				debug("make2", y + "," + tx);
//				printArray(temp3);
				temp2 = temp3;
			}
			ty = widget.getS();
		}
		
		if (ty != widget.getS()) {
			temp1 = table[1][tx];
			temp2 = table[ty][tx];
			for (y = ty + 1; y <= widget.getS(); y++) {
				temp3 = table[y][tx] = new int[temp2.length - 1][temp2[0].length];
				for (int i = 0; i < temp3.length; i++) {
					for (int j = 0; j < temp3[0].length; j++) {
						temp3[i][j] = temp2[i][j] + temp1[i + y - 1][j];
					}
				}
//				debug("make3", y + "," + tx);
//				printArray(temp3);
				temp2 = temp3;
			}
			ty = widget.getS();
		} else if (tx != widget.getT()) {
			temp1 = table[ty][1];
			temp2 = table[ty][tx];
			for (x = tx + 1; x <= widget.getT(); x++) {
				temp3 = table[ty][x] = new int[temp2.length][temp2[0].length - 1];
				for (int i = 0; i < temp3.length; i++) {
					for (int j = 0; j < temp3[0].length; j++) {
						temp3[i][j] = temp2[i][j] + temp1[i][j + x - 1];
					}
				}
//				debug("make4", ty + "," + x);
//				printArray(temp3);
				temp2 = temp3;
			}
			tx = widget.getT();
		}
		
		temp1 = table[ty][tx];
		int count = 0;
		for (int i = 0; i < temp1.length; i++) {
			for (int j = 0; j < temp1[0].length; j++) {
				if (temp1[i][j] == 0) {
					count++;
				}
			}
		}
		cache[ty][tx] = count + 1;
		return count;
	}

}

final class Paiza
{
	public static abstract class Resolver
	{
		protected Paiza.Home home;
		
		public void setHome(Paiza.Home home) {
			this.home = home;
		}
	
		public abstract int resolve(Paiza.Widget widget);
	}
	
	public static Paiza getInstance() throws java.lang.Exception {
		if (paiza == null) {
			paiza = new Paiza();
		}
		return paiza;
	}	

	public void resolve(Resolver resolver) {
		init(resolver);
		for (Widget widget : widgets) {
			answer(resolver.resolve(widget));
		}
		flush();
	}

	
	public final class Home
	{
		private final int H;
		private final int W;
		private int[][] display;
		private Home(int H, int W) {
			this.H = H;
			this.W = W;
			display = new int[H][W];
		}
		
		private void setDisplay(int x, int y, int e) {
			display[y][x] = e;
		}
		
		public int getH() {
			return H;
		}
		
		public int getW() {
			return W;
		}
		
		public boolean isSpace(int x, int y) {
			return display[y][x] == 0;
		}
	}
	
	public final class Widget
	{
		private final int s;
		private final int t;
		private Widget(int s, int t) {
			this.s = s;
			this.t = t;
		}
		
		public int getS() {
			return s;
		}
		
		public int getT() {
			return t;
		}
	}
	
	private static Paiza paiza = null;
	
	private Home home;
	private ArrayList<Widget> widgets;
	
	private Paiza() throws java.lang.Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String[] hw = in.readLine().split(" ");
		home = new Home(Integer.parseInt(hw[0]), Integer.parseInt(hw[1]));
		for (int y = 0; y < home.getH(); y++) {
			String line = in.readLine();
			for (int x = 0; x < home.getW(); x++) {
				home.setDisplay(x, y, (int)(line.charAt(x) - '0'));
			}
		}
		int N = Integer.parseInt(in.readLine());
		widgets = new ArrayList<Widget>(N);
		for (int i = 0; i< N; i++) {
			String[] st = in.readLine().split(" ");
			widgets.add(new Widget(Integer.parseInt(st[0]), Integer.parseInt(st[1])));
		}
	}
	
	private StringBuilder output = null;
	private static final String NEWLINE = System.getProperty("line.separator");
	
	private void init(Resolver resolver) {
		resolver.setHome(home);
		output = new StringBuilder(widgets.size() * 6);
	}
	
	private void answer(int count) {
		output.append(count);
		output.append(NEWLINE);
	}

	private void flush() {
		System.out.print(output);
	}
}
