/* paiza POH! vol.2
* result:
* http://p...content-available-to-author-only...a.jp/poh/paizen/result/bf7a5ec80e12edba64c30dabdd93a807
* author: Leonardone @ NEETSDKASU
*/
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
{
String[] hw
= in.
readLine().
split(" "); final int H
= Integer.
parseInt(hw
[0]); // ホーム画面縦の区画数 final int W
= Integer.
parseInt(hw
[1]); // ホーム画面横の区画数
int[][] space2left = new int[H][W];
int[][] space2top = new int[H][W];
int spacecount = 0;
int splCount = 0;
int sptCount = 0;
for (int y = 0; y < H; y++)
{
int space2leftSize = 0;
for (int x = 0; x < W; x++)
{
char ch = line.charAt(x);
if (ch == '1')
{
space2leftSize = 0;
space2top[y][x] = 0;
if (x > 0 && space2left[y][x - 1] > 0)
{
startposition2left[splCount++] =
}
if (y > 0 && space2top[y - 1][x] > 0)
{
startposition2top[sptCount++] =
}
}
else // if (ch == '0')
{
spacecount++;
space2leftSize++;
if (y > 0)
{
space2top[y][x] = space2top[y - 1][x] + 1;
if (y == H - 1)
{
startposition2top[sptCount++] =
}
}
else // if (y == 0)
{
space2top[y][x] = 1;
}
if (x == W - 1)
{
startposition2left[splCount++] =
}
}
space2left[y][x] = space2leftSize;
}
}
// print2dArray(space2left);
// print2dArray(space2top);
// System.out.println(startposition2left);
// System.out.println(startposition2top);
final int N
= Integer.
valueOf(in.
readLine()); // ウィジェット数 HashSet<Position> placeablePosition = new HashSet<Position>(H * W);
for (int i = 0; i < N; i++)
{
String[] st
= in.
readLine().
split(" "); int s
= Integer.
parseInt(st
[0]); // ウィジェットの縦サイズ int t
= Integer.
parseInt(st
[1]); // ウィジェットの横サイズ if ((value = result.get(key)) != null)
{
System.
out.
println(value.
intValue()); continue;
}
int placeableCount = 0;
if (s == 1) // 横長のウィジェット
{
if (t == 1) // 1x1のウィジェット
{
placeableCount = spacecount;
}
else // if (t != 1) // 横長のウィジェット
{
for (int spl = 0; spl < splCount; ++spl)
{
Position position
= startposition2left
[spl
]; int spaceWSize = space2left[position.y][position.x];
if (spaceWSize >= t)
{
placeableCount += spaceWSize - t + 1;
}
}
}
}
else if (t == 1) // 縦長のウィジェット
{
for (int spt = 0; spt < sptCount; ++spt)
{
Position position
= startposition2top
[spt
]; int spaceHSize = space2top[position.y][position.x];
if (spaceHSize >= s)
{
placeableCount += spaceHSize - s + 1;
}
}
}
else // if (s != 1 && t != 1)
{
placeablePosition.clear();
// 横に走査していく
for (int spl = 0; spl < splCount; ++spl)
{
Position position
= startposition2left
[spl
]; int spaceWSize = space2left[position.y][position.x];
for (int dx = 0; dx < spaceWSize; ++dx)
{
int x = position.x - dx;
int spaceHSize = space2top[position.y][x];
if (spaceHSize < s)
{
continue; // 縦サイズが足りない
}
// 縦に走査して横サイズ以上の連続する空所を数える
int count = 0;
for (int dy = 0; dy < spaceHSize; ++dy)
{
int y = position.y - dy;
if (space2left[y][x] >= t)
{
count++;
if (count >= s) // ウィジェットが確保できる
{
boolean placed = placeablePosition.add(
if (placed == false)
{
break; // これ以上の縦の走査は走査済み
}
}
}
else
{
count = 0; // 連続の途切れ
}
}
}
}
placeableCount = placeablePosition.size();
}
System.
out.
println(placeableCount
); result.
put(key,
Integer.
valueOf(placeableCount
)); }
} // end of main(String[])
static void print2dArray(int[][] array)
{
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
System.
out.
print(array
[i
][j
] + " "); }
}
}
{
static Position getPosition
(int y,
int x
) {
int index = y * 300 + x;
if (pool[index] == null)
{
}
return pool[index];
}
final int y;
final int x;
{
this.y = y;
this.x = x;
}
@Override
public String toString
() {
return "(" + x + ", " + y + ")";
}
@Override public int hashCode()
{
return y ^ x;
}
@Override
public boolean equals
(Object obj
) {
return y == pos.y && x == pos.x;
}
} // end of class Position
}