//#define _GLIBCXX_DEBUG #include "bits/stdc++.h" using namespace std; //------------------------------- Libraries --------------------------------// //------------------------------- Type Names -------------------------------// using i64 = int_fast64_t; using seika = string; //akari : 1D, yukari : 2D, maki : 3D vector template using akari = vector; template using yukari = akari>; template using maki = akari>; //akane : ascending order, aoi : decending order template using akane = priority_queue, greater>; template using aoi = priority_queue; //------------------------------- Dubug Functions ---------------------------// inline void print() { cout << endl; } template void print(const First &first, const Rest &... rest) { cout << first << ' '; print(rest...); } //------------------------------- Solver ------------------------------------// void solve() { int n, m; cin >> n >> m; yukari grid(m + 1, akari(m + 1)); for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) { cin >> grid[i + 1][j + 1]; } for (int i = 0; i <= m; i++) for (int j = 0; j < m; j++) { grid[i][j + 1] += grid[i][j]; } for (int i = 0; i < m; i++) for (int j = 0; j <= m; j++) { grid[i + 1][j] += grid[i][j]; } yukari imos(m + 1, akari(m + 1)); for (int lx = 0; lx < m; lx++) for (int ly = 0; ly < m; ly++) for (int rx = lx; rx < m; rx++) for (int ry = ly; ry < m; ry++) { i64 sum = 0; sum += grid[ly][lx]; sum -= grid[ry + 1][lx]; sum -= grid[ly][rx + 1]; sum += grid[ry + 1][rx + 1]; if (sum == 0) { imos[lx][ly]++; imos[lx][ry + 1]--; imos[rx + 1][ly]--; imos[rx + 1][ry + 1]++; } } for (int i = 0; i <= m; i++) for (int j = 0; j < m; j++) { imos[i][j + 1] += imos[i][j]; } for (int i = 0; i < m; i++) for (int j = 0; j <= m; j++) { imos[i + 1][j] += imos[i][j]; } while (n--) { int x, y; cin >> x >> y; x--, y--; cout << imos[y][x] << endl; } } int main() { solve(); return 0; }