/* paiza POH! vol.2
* result:
* http://p...content-available-to-author-only...a.jp/poh/paizen/result/a696b103c76284d247e6b48cb5285069
* author: Leonardone @ NEETSDKASU
*/
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
{
String[] hw
= in.
readLine().
split(" "); int H
= Integer.
parseInt(hw
[0]); // ホーム画面縦の区画数 int W
= Integer.
parseInt(hw
[1]); // ホーム画面横の区画数
int[][] home = new int[H][W];
int[][] result = new int[301][301];
int count = 0;
for (int y = 0; y < H; y++)
{
for (int x = 0; x < W; x++)
{
if ((home[y][x] = (int)(line.charAt(x) - '0')) == 0)
{
count++;
}
}
}
result[1][1] = count;
int[][][] table = new int[301][][];
int right;
int ww = W, hh;
int[][] temp1, temp2;
temp1 = table[1] = home;
for (right = 2; right <= W; right++)
{
count = 0;
ww--;
temp2 = table[right] = new int[H][ww];
for (int y = 0; y < H; y++)
{
for (int x = 0; x < ww; x++)
{
if ((temp2[y][x] = temp1[y][x] + temp1[y][x + 1]) == 0)
{
count++;
}
}
}
result[1][right] = count;
if (count < 2)
{
break;
}
temp1 = temp2;
}
ww = W + 1;
for (int i = 1; i < right; i++)
{
ww--;
temp1 = table[i];
hh = H;
for (int bottom = 2; bottom <= H; bottom++)
{
count = 0;
hh--;
for (int y = 0; y < hh; y++)
{
for (int x = 0; x < ww; x++)
{
if ((temp1[y][x] += temp1[y + 1][x]) == 0)
{
count++;
}
}
}
result[bottom][i] = count;
if (count < 2)
{
break;
}
}
}
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]); // ウィジェットの横サイズ
System.
out.
println(result
[s
][t
]); }
} // end of main(String[])
static void print2DArray(int[][] array)
{
print2DArray(array, array.length, array[0].length);
}
static void print2DArray(int[][] array, int h, int w)
{
System.
out.
println("Array --------- "); for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
System.
out.
printf("%3d ", array
[i
][j
]); }
}
System.
out.
println("--------------- "); }
}