#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll=long long; using ld=long double; using P=pair; #define MOD 1000000007LL #define INF 1000000000LL #define EPS 1e-10 #define FOR(i,n,m) for(ll i=n;i<(ll)m;i++) #define REP(i,n) FOR(i,0,n) #define DUMP(a) REP(d,a.size()){cout< >& a) { _h=h; _w=w; _sum.resize(_h+1); for(int i=0; i<_h+1; ++i) { _sum[i].resize(_w+1); for(int j=0; j<_w+1; ++j) { if(i==0||j==0) _sum[i][j]=0; else _sum[i][j]=a[i-1][j-1]; } } for(int i=0; i<_h+1; ++i) { for(int j=1; j<_w+1; ++j) _sum[i][j]+=_sum[i][j-1]; } for(int i=0; i<_w+1; ++i) { for(int j=1; j<_h+1; ++j) _sum[j][i]+=_sum[j-1][i]; } } // the coordinates of ul and dr should be 0-indexed long long query(pair ul,pair dr) { ll ret=0; ret+=_sum[dr.first+1][dr.second+1]; ret-=_sum[dr.first+1][ul.second]; ret-=_sum[ul.first][dr.second+1]; ret+=_sum[ul.first][ul.second]; return ret; } private: int _h; int _w; vector > _sum; }; int main() { cin.tie(0); ios::sync_with_stdio(false); ll n, m; cin >> n >> m; vector> a(m,vector(m)); REP(i,m) REP(j,m) cin >> a[i][j]; AccumulSum acm(m, m, a); REP(query,n) { ll x, y; cin >> x >> y; x--; y--; ll ans = 0; for(ll i = 0; i <= x; i++) for(ll j = 0; j <= y; j++) { for(ll k = x; k < m; k++) for(ll l = y; l < m; l++) { if(acm.query(P(i, j), P(k, l)) == 0) ans++; } } cout << ans << endl; } return 0; } /* --------------------------------------- */