#include using namespace std; signed main() { ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector> A(M, vector(M)); for (int i = 0; i < M; ++i) for (int j = 0; j < M; ++j) cin >> A[i][j]; vector> sum(M, vector(M)); for (int i = 0; i < M; ++i) for (int j = 0; j < M; ++j) { sum[i][j] = A[i][j]; if (i) sum[i][j] += sum[i - 1][j]; if (j) sum[i][j] += sum[i][j - 1]; if (i && j) sum[i][j] -= sum[i - 1][j - 1]; } auto query = [&](int r1, int c1, int r2, int c2) { int64_t res = sum[r2][c2]; if (r1) res -= sum[r1 - 1][c2]; if (c1) res -= sum[r2][c1 - 1]; if (r1 && c1) res += sum[r1 - 1][c1 - 1]; return res; }; for (int i = 0; i < N; ++i) { int X, Y; cin >> X >> Y; --X, --Y; int ans = 0; for (int r1 = 0; r1 < M; ++r1) for (int c1 = 0; c1 < M; ++c1) for (int r2 = r1; r2 < M; ++r2) for (int c2 = c1; c2 < M; ++c2) if (r1 <= X && X <= r2 && c1 <= Y && Y <= c2) ans += query(r1, c1, r2, c2) == 0; cout << ans << endl; } return 0; }