#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)<; using vi = vector; using vll = vector; typedef ll SumType; class Sum2D { public: Sum2D() {} Sum2D(const vector> &a) :h((int)a.size()), w((int)a[0].size()), s(h + 1, vector(w + 1)) { for (int i = 1; i <= h; ++i) for (int j = 1; j <= w; ++j) s[i][j] = s[i - 1][j] + s[i][j - 1] + a[i - 1][j - 1] - s[i - 1][j - 1]; } SumType get(int sy, int sx, int ty, int tx) { return s[ty][tx] - s[sy][tx] - s[ty][sx] + s[sy][sx]; } private: int h, w; vector> s; }; void solve() { int N, M; cin >> N >> M; vector> A(M, vector(M)), T(M + 1, vector(M + 1)); rep(i, M)rep(j, M)cin >> A[i][j]; Sum2D S(A); rep(sy, M)rep(sx, M)FOR(ty, sy + 1, M + 1)FOR(tx, sx + 1, M + 1) { ll sm = S.get(sy, sx, ty, tx); if (sm == 0) { T[sy][sx]++; T[sy][tx]--; T[ty][sx]--; T[ty][tx]++; } } rep(i, M)rep(j, M)T[i][j + 1] += T[i][j]; rep(i, M)rep(j, M)T[i + 1][j] += T[i][j]; rep(i, N) { int x, y; cin >> x >> y; --x; --y; cout << T[x][y] << endl; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); solve(); return 0; }