#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i, m, n) for(int i=int(m);i inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } //改造 typedef long long int ll; using namespace std; #define INF (1 << 30) - 1 #define INFl (ll)5e15 #define DEBUG 0 //デバッグする時1にしてね #define dump(x) cerr << #x << " = " << (x) << endl #define MOD 1000000007 //ここから編集する template class Sum2d { vector > vv; unsigned xSize{}; unsigned ySize{}; public: Sum2d(unsigned xSize, unsigned ySize) { vector > vv(xSize + 2, vector(ySize + 2)); this->vv = vv; this->xSize = xSize + 2; this->ySize = ySize + 2; } void add(int x, int y, T value) { x++; y++; this->vv[x][y] += value; } void set_sum() { for (int i = 0; i < xSize - 1; i++) { for (int j = 0; j < ySize; j++) { this->vv[i + 1][j] += vv[i][j]; } } for (int i = 0; i < xSize; i++) { for (int j = 0; j < ySize - 1; j++) { this->vv[i][j + 1] += vv[i][j]; } } } //[sx,tx],[sy,ty]の範囲を求めるZE T calc(int sx, int sy, int tx, int ty) { tx++; ty++; return this->vv[tx][ty] + this->vv[sx][sy] - this->vv[tx][sy] - this->vv[sx][ty]; } }; template class Imos2d { vector > vv; unsigned xSize{}; unsigned ySize{}; public: Imos2d(unsigned xSize, unsigned ySize) { vector > vv(xSize + 2, vector(ySize + 2)); this->vv = vv; this->xSize = xSize + 2; this->ySize = ySize + 2; } //[sx,tx),[sy,ty)の範囲をvalueで埋める void add(int sx, int sy, int tx, int ty, T value) { sx++; sy++; tx++; ty++; this->vv[sx][sy] += value; this->vv[tx][ty] += value; this->vv[sx][ty] -= value; this->vv[tx][sy] -= value; } void set_sum() { for (int i = 0; i < xSize - 1; i++) { for (int j = 0; j < ySize; j++) { this->vv[i + 1][j] += vv[i][j]; } } for (int i = 0; i < xSize; i++) { for (int j = 0; j < ySize - 1; j++) { this->vv[i][j + 1] += vv[i][j]; } } } T calc(int x, int y) { return this->vv[x + 1][y + 1]; } }; class Solve { public: void input() { } void solve() { input(); int N, M; cin >> N >> M; vector > A(M, vector(M)); Sum2d sum2d(M, M); rep(i, 0, M) { rep(j, 0, M) { cin >> A[i][j]; sum2d.add(i, j, A[i][j]); } } sum2d.set_sum(); Imos2d imos2d(M, M); for (int sx = 0; sx < M; ++sx) { for (int sy = 0; sy < M; ++sy) { for (int tx = sx; tx < M; ++tx) { for (int ty = sy; ty < M; ++ty) { if (sum2d.calc(sx, sy, tx, ty) == 0) { imos2d.add(sx, sy, tx + 1, ty + 1, 1); } } } } } imos2d.set_sum(); rep(i, 0, N) { int x, y; cin >> x >> y; --x, --y; int ans = imos2d.calc(x, y); cout << ans << endl; } } }; int main() { cin.tie(0); ios::sync_with_stdio(false); Solve().solve(); return 0; }