#include using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) begin(v),end(v) template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using pii = pair; constexpr ll INF = 1ll<<30; constexpr ll longINF = 1ll<<60; constexpr ll MOD = 1000000007; constexpr bool debug = false; //---------------------------------// int main() { int N, M; cin >> N >> M; vector A(M, vector(M, 0)); REP(i, M) REP(j, M) scanf("%d", &A[i][j]); vector sum(M + 1, vector(M + 1, 0ll)); REP(i, M) REP(j, M) sum[i + 1][j + 1] = A[i][j]; REP(i, M + 1) REP(j, M) sum[i][j + 1] += sum[i][j]; REP(i, M) REP(j, M + 1) sum[i + 1][j] += sum[i][j]; vector ans(M + 1, vector(M + 1, 0)); REP(y1, M) FOR(y2, y1 + 1, M + 1) REP(x1, M) FOR(x2, x1 + 1, M + 1) { int s = sum[y2][x2] - sum[y1][x2] - sum[y2][x1] + sum[y1][x1]; if (s == 0) { ++ans[y1][x1]; --ans[y1][x2]; --ans[y2][x1]; ++ans[y2][x2]; } } REP(i, M + 1) REP(j, M) ans[i][j + 1] += ans[i][j]; REP(i, M) REP(j, M + 1) ans[i + 1][j] += ans[i][j]; REP(i, N) { int x, y; scanf("%d %d", &x, &y); printf("%d\n", ans[x - 1][y - 1]); } }