#include <iostream>
#include <map>

using namespace std;

int n, m;
long long a[130][130];
long long S[131][131];

long long f(int y1, int y2, int x1, int x2) {
  return S[y2][x2] - S[y2][x1] - S[y1][x2] + S[y1][x1];
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      S[i + 1][j + 1] = S[i + 1][j] + S[i][j + 1] - S[i][j] + a[i][j];
    }
  }
  while (n--) {
    int y, x;
    cin >> y >> x;
    y--; x--;
    long long ans = 0;
    for (int a = 0; a <= y; a++) {
      for (int b = y + 1; b <= m; b++) {
        map<long long, int> mp;
        for (int c = 0; c <= x; c++) {
          mp[f(a, b, c, x)]++;
        }
        for (int c = x + 1; c <= m; c++) {
          ans += mp[-f(a, b, x, c)];
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}