/* paiza POH! vol.2
* result:
* http://p...content-available-to-author-only...a.jp/poh/paizen/result/5c10e41d7057ffd16bd94690eac3db60
* 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;
@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) {
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;
for (int i = 1; i <= e; i++) {
tx = 0;
ty = 0;
x = widget.getT() - i + 1;
if (x > 0) {
for (y = widget.getS(); y > widget.getS() - i && y > 0; y--) {
if (table[y][x] != null) {
tx = x;
ty = y;
break;
}
}
}
y = widget.getS() - i + 1;
if (y > 0) {
for (x = widget.getT(); x > widget.getT() - i && x > 0; x--) {
if (table[y][x] != null) {
if (x + y > tx + ty) {
tx = x;
ty = y;
}
break;
}
}
}
if (tx != 0) {
break;
}
}
if (tx == 0 || ty == 0) {
tx = 1;
ty = 1;
}
if (ty != widget.getS()) {
temp1 = table[ty][tx];
for (y = ty + 1; y <= widget.getS(); y++) {
if (table[y][tx] != null) {
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];
}
}
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) {
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];
}
}
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;
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() {
}
}