#include using namespace std; // one-based numbering template struct CumulativeSum2D { private: vector< vector > _mat; int _col, _row; public: CumulativeSum2D(int col, int row) : _col(col), _row(row) { _mat.assign(_col+2, vector(_row+2, 0)); } int col() const { return _col; } int row() const { return _row; } vector &operator[](int i) { return _mat[i]; } Numeric &operator() (int i, int j) { return _mat[i][j]; } void Accumulate() { for (int i = 0; i <= _col; i++) { for (int j = 0; j <= _row; j++) { _mat[i][j+1] += _mat[i][j]; } } for (int i = 0; i <= _col; i++) { for (int j = 0; j <= _row; j++) { _mat[i+1][j] += _mat[i][j]; } } } Numeric rangeSum(int i, int j, int u, int v) { return _mat[i-1][j-1] + _mat[u][v] - _mat[i-1][v] - _mat[u][j-1]; } Numeric rangeSum(pair upper_left, pair lower_right) { return rangeSum(upper_left.first, upper_left.second, lower_right.first, lower_right.second); } void rangeAdd(int i, int j, int u, int v, Numeric add) { _mat[i][j] += add; _mat[u+1][v+1] += add; _mat[i][v+1] -= add; _mat[u+1][j] -= add; } void rangeAdd(pair upper_left, pair lower_right, Numeric add) { return rangeAdd(upper_left.first, upper_left.second, lower_right.first, lower_right.second, add); } void print() { for (int i = 0; i < _col+2; ++i) { for (int j = 0; j < _row+2; ++j) { cout << _mat[i][j] << "\t"; } cout << endl; } } }; int main() { int n, m; cin >> n >> m; CumulativeSum2D a(m, m), count(m, m); for (int i = 1; i <= m; ++i) { for (int j = 1; j <= m; ++j) { cin >> a[i][j]; } } a.Accumulate(); for (int i = 1; i <= m; ++i) { for (int j = 1; j <= m; ++j) { for (int u = i; u <= m; ++u) { for (int v = j; v <= m; ++v) { if (a.rangeSum({i, j}, {u, v}) == 0) { count.rangeAdd({i, j}, {u, v}, 1); } } } } } count.Accumulate(); for (int i = 0; i < n; ++i) { int p, q; cin >> p >> q; cout << count[p][q] << endl; } return 0; }