#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main(){ cin.tie(0); ios::sync_with_stdio(0); int N,M; cin >> N >> M; vector> A(M, vector(M)); rep(i,M)rep(j,M) cin >> A[i][j]; vector> A_sum(M + 1, vector(M + 1, 0)); rep(i,M)rep(j,M) A_sum[i + 1][j + 1] = A_sum[i][j + 1] + A_sum[i + 1][j] - A_sum[i][j] + A[i][j]; vector> imos(M + 1, vector(M + 1, 0)); rep(x,M)rep(y,M) { for(int i = x + 1; i <= M; i++) for(int j = y + 1; j <= M; j++) { ll SUM = A_sum[i][j] - A_sum[x][j] - A_sum[i][y] + A_sum[x][y]; if(SUM == 0LL) { imos[x][y]++; imos[x][j]--; imos[i][y]--; imos[i][j]++; } } } rep(i,M+1)rep(j,M) imos[i][j + 1] += imos[i][j]; rep(i,M)rep(j,M+1) imos[i + 1][j] += imos[i][j]; rep(i,N) { int x,y; cin >> x >> y; x--; y--; cout << imos[x][y] << "\n"; } }