#include using namespace std; using ll = long long; using ull = unsigned long long; #define ALL(obj) (obj).begin(),(obj).end() #define SPEED cin.tie(0);ios::sync_with_stdio(false); template using PQ = priority_queue; template using PQR = priority_queue,greater>; constexpr long long MOD = (long long)1e9 + 7; constexpr long long MOD2 = 998244353; constexpr long long HIGHINF = (long long)1e18; constexpr long long LOWINF = (long long)1e15; constexpr long double PI = 3.1415926535897932384626433; template vector multivector(size_t N,T init){return vector(N,init);} template auto multivector(size_t N,T... t){return vector(N,multivector(t...));} template void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}} template ostream &operator<<(ostream &o, const map&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;} template ostream &operator<<(ostream &o, const set&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template ostream &operator<<(ostream &o, const multiset&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template ostream &operator<<(ostream &o, const vector&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;} template ostream &operator<<(ostream &o, const pair&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;} void print(void) {cout << endl;} template void print(Head&& head) {cout << head;print();} template void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward(tail)...);} template void chmax(T& a, const T b){a=max(a,b);} template void chmin(T& a, const T b){a=min(a,b);} void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;} void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;} void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;} template class BinaryIndexedTree2D { //抽象化してない 一点加算、区間総和のみ const int N,M; vector> node; public: //1-indexed BinaryIndexedTree2D(const int& N,const int& M):N(N),M(M),node(N+1,vector(M+1,0)){ // do nothing } inline T getvar(const int i,const int j){ T res = 0; for(int y=i;y>0;y-=(y&-y)) for(int x=j;x>0;x-=(x&-x)) { res += node[y][x]; } return res; } // [x1,x2] * [y1,y2] 1-indexed inline T getvar(const int y1,const int y2,const int x1,const int x2){ T res = 0; res += getvar(y1-1,x1-1); res -= getvar(y1-1,x2); res -= getvar(y2,x1-1); res += getvar(y2,x2); return res; } //add 1-indexed inline void update(const int i,const int j,T val){ for(int y=i;y&&y<=N;y+=(y&-y)) for(int x=j;x&&x<=M;x+=(x&-x)) { node[y][x]+=val; } } }; //verify https://atcoder.jp/contests/abc130/tasks/abc130_e int main() { SPEED int N,M; cin >> N >> M; BinaryIndexedTree2D bit(M+1,M+1); auto sum = multivector(M+2,M+2,0LL); for(int i = 1; i <= M; ++i){ for(int j = 1; j <= M; ++j){ ll a; cin >> a; bit.update(i,j,a); } } for(int i = 1; i <= M; ++i){ for(int j = 1; j <= M; ++j){ for(int k = i; k <= M; ++k){ for(int l = j; l <= M; ++l){ if(bit.getvar(i,k,j,l)!=0) continue; sum[i][j]++; sum[i][l+1]--; sum[k+1][j]--; sum[k+1][l+1]++; } } } } for(int i = 0; i <= M; ++i){ for(int j = 0; j <= M; ++j){ sum[i][j+1] += sum[i][j]; } } for(int j = 0; j <= M; ++j){ for(int i = 0; i <= M; ++i){ sum[i+1][j] += sum[i][j]; } } for(int i = 0; i < N; ++i){ int x,y; cin >> x >> y; cout << sum[x][y] << endl; } return 0; }