/* paiza POH! vol.2
 * 
 * 
 * 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(" ");
		int H = Integer.parseInt(hw[0]); // ホーム画面縦の区画数
		int W = Integer.parseInt(hw[1]); // ホーム画面横の区画数
		
		int[][] home = new int[H][W];
		
		for (int y = 0; y < H; y++)
		{
			String line = in.readLine();
			for (int x = 0; x < W; x++)
			{
				home[y][x] = (int)(line.charAt(x) - '0');
			}
		}
		
		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]); // ウィジェットの横サイズ
			
			int count = 0;
			
			/* POH!Vol.2用テンプレ */

			System.out.println(count);
		}
		
	} // end of main(String[])
}
