#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define INF (1<<29) #define rep(i,n) for(int i=0;i<(int)(n);i++) #define all(v) v.begin(),v.end() #define uniq(v) v.erase(unique(all(v)),v.end()) int n, m; int a[130][130]; long long asum[131][130]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; rep(i,m)rep(j,m) { cin >> a[i][j]; asum[i+1][j] = a[i][j]; } rep(i,m) rep(j,m){ asum[i + 1][j] += asum[i][j]; } rep(i,n) { int x, y; cin >> x >> y; x--; y--; swap(x, y); long long ans = 0; for (int y1 = 0; y1 <= y;y1++) { for (int y2 = y; y2 < m;y2++) { long long centor = asum[y2 + 1][x] - asum[y1][x]; unordered_map lc, rc; long long s; s = centor; lc[centor] = 1; for (int x1 = x - 1; x1 >= 0; x1--) { s += asum[y2 + 1][x1] - asum[y1][x1]; lc[s]++; } s = 0; rc[0] = 1; for (int x2 = x + 1; x2 < m; x2++) { s += asum[y2 + 1][x2] - asum[y1][x2]; rc[s]++; } for (auto p : lc) { long long v = p.first; auto it = rc.find(-v); if (it != rc.end()) { ans += p.second * it->second; } } } } cout << ans << endl; } return 0; }