import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
public class Main {
public static class Widget {
public int widgetW;
public int widgetH;
public Widget(int w, int h) {
widgetW = w;
widgetH = h;
}
@Override
public boolean equals
(Object obj
) { if (obj instanceof Widget) {
Widget w = (Widget) obj;
return w.widgetW == widgetW && w.widgetH == widgetH;
}
return false;
}
@Override
public int hashCode() {
return widgetH * 65536 + widgetW;
}
}
int[][] space;
int w, h;
int[][] widgets;
HashMap<Widget, Integer> cache;
Main paizenMain = new Main();
paizenMain.run();
}
parseStdIn();
cache = new HashMap<Main.Widget, Integer>();
for (int i = 0; i < widgets.length; i++) {
Widget widget = new Widget(widgets[i][1], widgets[i][0]);
if (cache.containsKey(widget)) {
System.
out.
println(cache.
get(widget
)); } else {
int count = convolveAndCount(widgets[i]);
cache.put(widget, count);
}
}
}
private int convolveAndCount(int[] widget) {
int widgetW = widget[1];
int widgetH = widget[0];
if (w < widgetW || h < widgetH) {
return 0;
}
int[][] space2 = new int[h - widgetH + 1][w];
// row convolution
for (int i = 0; i < widgetH; i++) {
for (int y = 0; y < h - widgetH + 1; y++) {
for (int x = 0; x < w; x++) {
space2[y][x] += space[y + i][x];
}
}
}
// // check
// for (int i = 0; i < h - widgetH + 1; i++) {
// for (int j = 0; j < w; j++) {
// System.out.print(space2[i][j]);
// }
// System.out.println();
// }
int[][] space3 = new int[h - widgetH + 1][w - widgetW + 1];
// column convolution
for (int i = 0; i < widgetW; i++) {
for (int y = 0; y < h - widgetH + 1; y++) {
for (int x = 0; x < w - widgetW + 1; x++) {
space3[y][x] += space2[y][x + i];
}
}
}
// // check
// for (int i = 0; i < h - widgetH + 1; i++) {
// for (int j = 0; j < w - widgetW + 1; j++) {
// System.out.print(space2[i][j]);
// }
// System.out.println();
// }
// count
int count = 0;
for (int y = 0; y < h - widgetH + 1; y++) {
for (int x = 0; x < w - widgetW + 1; x++) {
if (space3[y][x] == 0)
count++;
}
}
return count;
}
int[] size = splitStrToIntArray(br.readLine());
h = size[0];
w = size[1];
space = new int[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
space[i][j] = line.charAt(j) - '0';
}
}
int widgetNum
= Integer.
parseInt(br.
readLine()); widgets = new int[widgetNum][2];
for (int i = 0; i < widgetNum; i++) {
widgets[i] = splitStrToIntArray(br.readLine());
}
// // check
// for (int i = 0; i < h; i++) {
// for (int j = 0; j < w; j++) {
// System.out.print(space[i][j]);
// }
// System.out.println();
// }
//
// for (int i = 0; i < widgetNum; i++) {
// System.out.println(widgets[i][0] + " " + widgets[i][1]);
// }
}
private int[] splitStrToIntArray
(String str
) { String[] strings
= str.
split("\\s"); int[] intArray = new int[strings.length];
for (int i = 0; i < strings.length; i++) {
intArray
[i
] = Integer.
parseInt(strings
[i
]); }
return intArray;
}
}