#include #include using namespace std; int main() { int n, m; cin >> n >> m; long long a[132][132] = {}; for (int i = 1; i <= m; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; for (int i = 1; i <= m; i++) for (int j = 1; j <= m; j++) a[i][j] += a[i][j-1]; for (int i = 1; i <= m; i++) for (int j = 1; j <= m; j++) a[i][j] += a[i-1][j]; while (n--) { int x, y; cin >> x >> y; long long ans = 0; for (int left = 1; left <= y; left++) for (int right = y; right <= m; right++) { map upper, lower; for (int i = 0; i < x; i++) upper[a[i][right] - a[i][left-1]]++; for (int i = x; i <= m; i++) ans += upper[a[i][right] - a[i][left-1]]; } cout << ans << endl; } }