/* paiza POH! vol.2
* result:
* http://p...content-available-to-author-only...a.jp/poh/paizen/result/79f911ad242151df16a8e6daa2885979
* 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;
}
@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) {
if (table[widget.getS()][widget.getT()] != null) {
if (widget.getS() != 1 || widget.getT() != 1) {
int f = 0;
if (widget.getS() < 300) {
if (table[widget.getS() + 1][widget.getT()] != null) {
f++;
}
} else {
f++;
}
if (widget.getT() < 300) {
if (table[widget.getS()][widget.getT() + 1] != null) {
f++;
}
} else {
f++;
}
if (f == 2) {
table[widget.getS()][widget.getT()] = null;
}
}
}
return cache[widget.getS()][widget.getT()] - 1;
}
return tatami(widget);
}
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 = 1; 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];
for (y = ty + 1; y <= widget.getS(); y++) {
if (table[y][tx] != null) {
printArray(table[y][tx], "skip1: " + y + "," + tx);
temp1 = table[y][tx];
continue;
}
temp2 = table[y][tx] = new int[temp1.length - 1][temp1[0].length];
for (int i = 0; i < temp2.length; i++) {
for (int j = 0; j < temp2[0].length; j++) {
temp2[i][j] = temp1[i][j] + temp1[i + 1][j];
}
}
printArray(temp2, "make1: " + y + "," + tx);
temp1 = temp2;
}
ty = widget.getS();
}
if (tx != widget.getT()) {
temp1 = table[ty][tx];
for (x = tx + 1; x <= widget.getT(); x++) {
if (table[ty][x] != null) {
printArray(table[ty][x], "skip2: " + ty + "," + x);
temp1 = table[ty][x];
continue;
}
temp2 = table[ty][x] = new int[temp1.length][temp1[0].length - 1];
for (int i = 0; i < temp2.length; i++) {
for (int j = 0; j < temp2[0].length; j++) {
temp2[i][j] = temp1[i][j] + temp1[i][j + 1];
}
}
printArray(temp2, "make2: " + ty + "," + x);
temp1 = temp2;
}
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;
int f = 0;
if (ty < 300) {
if (table[ty + 1][tx] != null) {
f++;
}
} else {
f++;
}
if (tx < 300) {
if (table[ty][tx + 1] != null) {
f++;
}
} else {
f++;
}
if (f == 2) {
table[ty][tx] = null;
}
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() {
}
}