/* paiza POH! vol.2
* result:
* http://p...content-available-to-author-only...a.jp/poh/paizen/result/271a1340e7c07149d62f3a321197dad4
* author: Leonardone @ NEETSDKASU
*/
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
{
Paiza.getInstance().resolve(new MyResolver());
}
}
{
int[][][][] table = null;
int[][] cache = null;
// System.out.println(key + " : " + str);
}
void printArray
(int[][] array,
String str
) { /*
System.out.println("----");
System.out.println("Array : " + array.length + "x" + array[0].length + " :: " + str);
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[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;
int dv = 5;
int ww = home.getW() / dv;
int[][] temp1;
if (ww > 1) {
temp = table[1][1];
for (int i = 1; i < ww; i++) {
int xx = i * dv;
temp1 = table[1][xx] = new int[temp.length][temp[0].length - dv + 1];
for (int y = 0; y < temp1.length; y++) {
for (int x = 0; x < temp1[0].length; x++) {
for (int j = 0; j < dv; j++) {
temp1[y][x] += temp[y][x + j];
}
}
}
printArray(temp1, "pre1: 1," + xx);
temp = temp1;
}
}
int hh = home.getH() / dv;
if (hh > 1) {
temp = table[1][1];
for (int i = 1; i < hh; i++) {
int yy = i * dv;
temp1 = table[yy][1] = new int[temp.length - dv + 1][temp[0].length];
for (int y = 0; y < temp1.length; y++) {
for (int x = 0; x < temp1[0].length; x++) {
for (int j = 0; j < dv; j++) {
temp1[y][x] += temp[y + j][x];
}
}
}
printArray(temp1, "pre2: " + yy + ",1");
temp = temp1;
}
for (int k = 1; k < ww; k++) {
int xx = k * dv;
temp = table[1][xx];
for (int i = 1; i < hh; i++) {
int yy = i * dv;
temp1 = table[yy][xx] = new int[temp.length - dv + 1][temp[0].length];
for (int y = 0; y < temp1.length; y++) {
for (int x = 0; x < temp1[0].length; x++) {
for (int j = 0; j < dv; j++) {
temp1[y][x] += temp[y + j][x];
}
}
}
printArray(temp1, "pre3: " + yy + "," + xx);
temp = temp1;
}
}
}
}
@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) {
release(widget.getS(), widget.getT());
return cache[widget.getS()][widget.getT()] - 1;
}
return tatami(widget);
}
private void release(int s, int t) {
if (s == 1 && t == 1) {
return;
}
if (table[s][t] == null || cache[s][t] == 0) {
return;
}
int f = 0;
if (s < 300) {
if (cache[s + 1][t] > 0 || table[s + 1][t] != null) {
f++;
}
} else {
f++;
}
if (t < 300) {
if (cache[s][t + 1] > 0 || table[s][t+ 1] != null) {
f++;
}
} else {
f++;
}
if (f == 2) {
debug("release", s + "," + t);
table[s][t] = null;
}
}
private int tatami(Paiza.Widget widget) {
int e
= Math.
max(widget.
getS(), widget.
getT()); int tx = 1, ty = 1, x, y;
int[][] temp1, temp2;
debug("tatami", widget.getS() + "," + widget.getT());
for (int i = 0; i <= e; i++) {
tx = 0;
ty = 0;
y = widget.getS();
x = widget.getT() - i;
if (x < 1) {
y -= - x + 1;
x = 1;
}
while (x <= widget.getT() && y > 0) {
debug("seek", y + "," + x);
if (table[y][x] != null) {
tx = x;
ty = y;
break;
}
x++;
y--;
}
if (tx != 0) {
break;
}
}
if (tx == 0 || ty == 0) {
tx = 1;
ty = 1;
}
printArray(table[ty][tx], "tytx: " + ty + "," + tx);
if (ty != widget.getS()) {
temp1 = table[ty][tx];
temp2 = table[widget.getS()][tx] = new int[temp1.length - widget.getS() + ty][temp1[0].length];
for (y = 0; y < temp2.length; y++) {
for (x = 0; x < temp2[0].length; x++) {
for (int j = 0; j < widget.getS() - ty + 1; j++) {
temp2[y][x] += temp1[y + j][x];
}
}
}
ty = widget.getS();
printArray(temp2, "make1: " + ty + "," + tx);
}
if (tx != widget.getT()) {
temp1 = table[ty][tx];
temp2 = table[ty][widget.getT()] = new int[temp1.length][temp1[0].length - widget.getT() + tx];
for (y = 0; y < temp2.length; y++) {
for (x = 0; x < temp2[0].length; x++) {
for (int j = 0; j < widget.getT() - tx + 1; j++) {
temp2[y][x] += temp1[y][x + j];
}
}
}
tx = widget.getT();
printArray(temp2, "make2: " + ty + "," + tx);
}
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;
release(ty, tx);
return count;
}
}
final class Paiza
{
{
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;
String[] hw
= in.
readLine().
split(" "); for (int y = 0; y < home.getH(); y++) {
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");
resolver.setHome(home);
output = new StringBuilder(widgets.size() * 6);
}
private void answer(int count) {
output.append(count);
output.append(NEWLINE);
}
private void flush() {
}
}