#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define ll long long #define ld long double #define EPS 0.0000000001 #define INF 1e9 #define LINF (ll)INF*INF #define MOD 1000000007 #define rep(i,n) for(int i=0;i<(n);i++) #define loop(i,a,n) for(int i=a;i<(n);i++) #define all(in) in.begin(),in.end() #define shosu(x) fixed< vi; typedef vector vs; typedef pair pii; typedef vector vp; int gcd(int a, int b){ if(b==0) return a; return gcd(b,a%b); } int lcm(int a, int b){ return a/gcd(a,b)*b; } signed main(void) { int n,m; cin >> n >> m; vector a(m+1,vi(m+1)); rep(i,m)rep(j,m)cin >> a[i+1][j+1]; vector in = a; rep(i,m+1)rep(j,m)a[i][j+1] += a[i][j]; rep(j,m+1)rep(i,m)a[i+1][j] += a[i][j]; //rep(i,m+1){rep(j,m+1)cout << " " << a[i][j];cout << endl;} rep(t,n){ int x,y; cin >> x >> y; int ans = 0; loop(i,1,x+1)loop(j,max(i,x),m+1){ if(i != j)ans += a[j][y] + a[i-1][y-1] == a[i-1][y] + a[j][y-1]; else ans += in[x][y] == 0; vi sum(y); loop(k,1,y)sum[k-1] = a[j][k] - a[i-1][k]; sort(all(sum)); loop(k,y,m+1){ int tmp = a[j][k] - a[i-1][k]; ans += upper_bound(all(sum),tmp) - lower_bound(all(sum),tmp); } //cout << i << " " << j << " " << ans << endl; } cout << ans << endl; } }