#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 using namespace std; using ll = long long; using P = pair; constexpr int INF = 1001001001; constexpr int mod = 1000000007; // constexpr int mod = 998244353; template inline bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } template inline bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } template struct CumulativeSum2D{ vector> data; CumulativeSum2D(int W, int H) : data(W + 1, vector(H + 1, 0)) {} void add(int x, int y, T z){ ++x, ++y; if(x >= data.size() || y >= data[0].size()) return; data[x][y] += z; } void build(){ for(int i = 1; i < (int)data.size(); ++i){ for(int j = 1; j < (int)data[i].size(); ++j){ data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1]; } } } T query(int sx, int sy, int gx, int gy){ return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]); } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; cin >> N >> M; CumulativeSum2D S(M, M); for(int i = 0; i < M; ++i){ for(int j = 0; j < M; ++j){ ll A; cin >> A; S.add(i, j, A); } } vector X(N), Y(N); for(int i = 0; i < N; ++i){ cin >> X[i] >> Y[i]; --X[i], --Y[i]; } S.build(); vector ans(N); for(int sx = 0; sx < M; ++sx){ for(int sy = 0; sy < M; ++sy){ for(int gx = sx + 1; gx <= M; ++gx){ for(int gy = sy + 1; gy <= M; ++gy){ if(S.query(sx, sy, gx, gy) == 0LL){ for(int i = 0; i < N; ++i){ if(sx <= X[i] && X[i] < gx && sy <= Y[i] && Y[i] < gy){ ans[i] += 1; } } } } } } } for(int i = 0; i < N; ++i) cout << ans[i] << '\n'; return 0; }