// POH Vol.2
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <climits>
using namespace std;
int main(int argc, char** argv) {
const int MAX_SIZE = 300;
int h, w; // h = 画面縦区画数、w = 画面横区画数
cin >> h >> w;
cin.ignore(); //改行文字を1文字読み飛ばす
// vv[y][x] = 左から数えた連続した空きの数(空いてないマスは0)
vector<vector<int> > vv(h, vector<int>(w, 0));
// ans[s-1][t-1] = 縦s横tのウィジットの配置可能数
vector<vector<int> > ans(MAX_SIZE, vector<int>(MAX_SIZE, 0));
for(int y=0; y<h; ++y) {
string str;
getline(cin, str);
int count = 0;
for(int x=0; x<w; ++x) {
if(str[x] == '1') {
count = 0;
continue;
}
++count;
vv[y][x] = count;
int dx = INT_MAX;
for(int dy=0; y-dy>=0; ++dy) {
dx = std::min(dx, vv[y-dy][x]);
for(int t=dx-1; t>=0; --t) {
++ans[dy][t];
}
}
}
}
int n, s, t; //n=ウィジットの数、s=縦、t=横
cin >> n;
for(int i=0; i<n; ++i) {
cin >> s >> t;
cout << ans[s-1][t-1] << endl;
}
return 0;
}
Ly8gUE9IIFZvbC4yCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGNsaW1pdHM+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbihpbnQgYXJnYywgY2hhcioqIGFyZ3YpIHsKCWNvbnN0IGludCBNQVhfU0laRSA9IDMwMDsKCWludCBoLCB3OyAvLyBoID0g55S76Z2i57im5Yy655S75pWw44CBdyA9IOeUu+mdouaoquWMuueUu+aVsAoJY2luID4+IGggPj4gdzsKCWNpbi5pZ25vcmUoKTsgLy/mlLnooYzmloflrZfjgpIx5paH5a2X6Kqt44G/6aOb44Gw44GZCgoJLy8gdnZbeV1beF0gPSDlt6bjgYvjgonmlbDjgYjjgZ/pgKPntprjgZfjgZ/nqbrj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