#include #define rep(i,a,b) for(int i=a;i=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() #pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b struct Ruisekiwa2D { ll v[VH][VW]; void set(int x, int y, ll c) { v[y][x] = c; } void build() { rep(y, 0, VH) rep(x, 0, VW) { if (0 < y) v[y][x] += v[y - 1][x]; if (0 < x) v[y][x] += v[y][x - 1]; if (0 < y && 0 < x) v[y][x] -= v[y - 1][x - 1]; } } ll get(int sx, int sy, int tx, int ty) { assert(sx <= tx && sy <= ty); ll rs = v[ty][tx]; if (0 < sx) rs -= v[ty][sx - 1]; if (0 < sy) rs -= v[sy - 1][tx]; if (0 < sx && 0 < sy) rs += v[sy - 1][sx - 1]; return rs; } }; /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N, M, A[150][150]; Ruisekiwa2D<150, 150> r2d; int ans[150][150]; //--------------------------------------------------------------------------------------------------- void _main() { cin >> N >> M; rep(y, 0, M) rep(x, 0, M) cin >> A[y][x]; rep(y, 0, M) rep(x, 0, M) r2d.set(y, x, A[y][x]); r2d.build(); rep(sy, 0, M) rep(ty, sy, M) rep(sx, 0, M) rep(tx, sx, M) { if (r2d.get(sx, sy, tx, ty) == 0) { ans[sy][sx]++; ans[ty + 1][sx]--; ans[sy][tx + 1]--; ans[ty + 1][tx + 1]++; } } rep(y, 0, M) rep(x, 1, M) ans[y][x] += ans[y][x - 1]; rep(x, 0, M) rep(y, 1, M) ans[y][x] += ans[y - 1][x]; rep(i, 0, N) { int x, y; cin >> x >> y; x--; y--; printf("%d\n", ans[y][x]); } }