#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; int main() { int N, M; cin >> N >> M; ll A[M][M]; for (int i = 0; i < M; ++i) { for (int j = 0; j < M; ++j) { cin >> A[i][j]; } } ll S[M + 1][M + 1]; memset(S, 0, sizeof(S)); for (int y = 1; y <= M; ++y) { for (int x = 1; x <= M; ++x) { S[y][x] = S[y][x - 1] + A[y - 1][x - 1]; } } for (int x = 0; x <= M; ++x) { for (int y = 1; y <= M; ++y) { S[y][x] += S[y - 1][x]; } } /* for (int y = 0; y <= M; ++y) { for (int x = 0; x <= M; ++x) { cerr << S[y][x] << " "; } cerr << endl; } */ int x, y; for (int i = 0; i < N; ++i) { cin >> y >> x; int ans = 0; for (int cy = 1; cy <= y; ++cy) { for (int cx = 1; cx <= x; ++cx) { for (int ny = y; ny <= M; ++ny) { for (int nx = x; nx <= M; ++nx) { if (ny < cy) continue; if (nx < cx) continue; int ly = min(cy, ny); int lx = min(cx, nx); int ry = max(cy, ny); int rx = max(cx, nx); if (ly <= y && y <= ry && lx <= x && x <= rx) { ll sum = S[ry][rx] - S[ry][lx - 1] - S[ly - 1][rx] + S[ly - 1][lx - 1]; // fprintf(stderr, "(%d, %d) -> (%d, %d) sum: %lld\n", lx, ly, rx, ry, sum); if (sum == 0) ++ans; } } } } } cout << ans << endl; } return 0; }