#include using namespace std; typedef long long signed int LL; typedef long long unsigned int LU; #define incID(i, l, r) for(int i = (l) ; i < (r); i++) #define incII(i, l, r) for(int i = (l) ; i <= (r); i++) #define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--) #define decII(i, l, r) for(int i = (r) ; i >= (l); i--) #define inc(i, n) incID(i, 0, n) #define inc1(i, n) incII(i, 1, n) #define dec(i, n) decID(i, 0, n) #define dec1(i, n) decII(i, 1, n) #define inII(v, l, r) ((l) <= (v) && (v) <= (r)) #define inID(v, l, r) ((l) <= (v) && (v) < (r)) #define PB push_back #define EB emplace_back #define MP make_pair #define FI first #define SE second #define PQ priority_queue #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() #define FOR(it, v) for(auto it = v.begin(); it != v.end(); ++it) #define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it) template bool setmin(T & a, T b) { if(b < a) { a = b; return true; } else { return false; } } template bool setmax(T & a, T b) { if(b > a) { a = b; return true; } else { return false; } } template bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } } template bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } } template T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); } template T lcm(T a, T b) { return a / gcd(a, b) * b; } // ---- ---- template class Sum2 { private: vector> a, s; int I, J; bool ok; public: Sum2() { I = J = -1; } Sum2(int i, int j) { init(i, j); } void init(int i, int j) { I = i; J = j; a = vector>(I + 0, vector(J + 0, 0)); s = vector>(I + 1, vector(J + 1, 0)); } vector & operator[](int i) { assert(inID(i, 0, I)); ok = false; return a[i]; } void calc() { inc(i, I) { inc(j, J) { s[i + 1][j + 1] = a[i][j] + s[i + 1][j] + s[i][j + 1] - s[i][j]; } } ok = true; } T sum(int il, int ih, int jl, int jh) { // [il, ih) × [jl, jh) assert(ok); assert(inII(il, 0, I)); assert(inII(ih, 0, I)); assert(inII(jl, 0, J)); assert(inII(jh, 0, J)); assert(il <= ih); assert(jl <= jh); return s[ih][jh] - s[il][jh] - s[ih][jl] + s[il][jl]; } const vector> & get_a() { return a; } const vector> & get_s() { return s; } }; // ---- int main() { int n, m; cin >> n >> m; Sum2 s(m, m); inc(i, m) { inc(j, m) { cin >> s[i][j]; } } s.calc(); inc(q, n) { int i, j; cin >> i >> j; int ans = 0; incID(il, 0, i) { incII(ih, i, m) { incID(jl, 0, j) { incII(jh, j, m) { if(s.sum(il, ih, jl, jh) == 0) { ans++; } } } } } cout << ans << endl; } return 0; }