#include "bits/stdc++.h" #define ALL(obj) (obj).begin(),(obj).end() #define RALL(obj) (obj).rbegin(),(obj).rend() #define REP(i, n) for(int i = 0; i < (int)(n); i++) #define REPR(i, n) for(int i = (int)(n); i >= 0; i--) #define FOR(i,n,m) for(int i = (int)(n); i < int(m); i++) using namespace std; typedef long long ll; const int MOD = 1e9 + 7; const int INF = MOD - 1; const ll LLINF = 4e18; int main() { int n, m; cin >> n >> m; vector> a(m, vector(m)); vector> s(m, vector(m)); vector> ans(m, vector(m, 0)); REP(i, m) { REP(j, m) { cin >> a[i][j]; } } REP(i, m) { REP(j, m) { s[i][j] = a[i][j]; if (i > 0) s[i][j] += s[i - 1][j]; if (j > 0) s[i][j] += s[i][j - 1]; if (i > 0 && j > 0) s[i][j] -= s[i - 1][j - 1]; } } REP(i1, m) { REP(j1, m) { FOR(i2, i1, m) { FOR(j2, j1, m) { if (i1 > 0) { if (j1 > 0) { if (s[i2][j2] + s[i1 - 1][j1 - 1] - s[i2][j1 - 1] - s[i1 - 1][j2] == 0) { ans[i1][j1] += 1; if (i2 < m - 1) ans[i2 + 1][j1] -= 1; if (j2 < m - 1) ans[i1][j2 + 1] -= 1; if (i2 < m - 1 && j2 < m - 1) ans[i2 + 1][j2 + 1] += 1; } } else { if (s[i2][j2] - s[i1 - 1][j2] == 0) { ans[i1][j1] += 1; if (i2 < m - 1) ans[i2 + 1][j1] -= 1; if (j2 < m - 1) ans[i1][j2 + 1] -= 1; if (i2 < m - 1 && j2 < m - 1) ans[i2 + 1][j2 + 1] += 1; } } } else { if (j1 > 0) { if (s[i2][j2] - s[i2][j1 - 1] == 0) { ans[i1][j1] += 1; if (i2 < m - 1) ans[i2 + 1][j1] -= 1; if (j2 < m - 1) ans[i1][j2 + 1] -= 1; if (i2 < m - 1 && j2 < m - 1) ans[i2 + 1][j2 + 1] += 1; } } else { if (s[i2][j2] == 0) { ans[i1][j1] += 1; if (i2 < m - 1) ans[i2 + 1][j1] -= 1; if (j2 < m - 1) ans[i1][j2 + 1] -= 1; if (i2 < m - 1 && j2 < m - 1) ans[i2 + 1][j2 + 1] += 1; } } } } } } } REP(i, m) { REP(j, m - 1) { ans[i][j + 1] += ans[i][j]; } } REP(i, m) { REP(j, m - 1) { ans[j + 1][i] += ans[j][i]; } } REP(i, n) { int x, y; cin >> x >> y; x--; y--; cout << ans[x][y] << endl; } getchar(); getchar(); }