#include using namespace std; #define _p(...) (void)printf(__VA_ARGS__) #define forr(x,arr) for(auto&& x:arr) #define _overload3(_1,_2,_3,name,...) name #define _rep2(i,n) _rep3(i,0,n) #define _rep3(i,a,b) for(int i=int(a);i=int(a);i--) #define rrep(...) _overload3(__VA_ARGS__,_rrep3,_rrep2,)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define bit(n) (1LL<<(n)) #define sz(x) ((int)(x).size()) #define fst first #define snd second using ll=long long;using pii=pair;using vb=vector; using vi=vector;using vvi=vector;using vvvi=vector; using vl=vector;using vvl=vector;using vvvl=vector; using vd=vector;using vvd=vector;using vvvd=vector; using vpii=vector;using vvpii=vector;using vvvpii=vector; template T read() {T t; cin >> t; return t;} void Main() { int H = read(); int W = read(); vector B(H); rep(i, H) B[i] = read(); int Q = read(); vvi R(H+1, vi(W+1)); vvi csy(H); vvi csx(W); rep(y, H) rep(x, W) { if (B[y][x] == 'w') { R[y][x] = 1; } else { csy[y].push_back(x); csx[x].push_back(y); } } //rep(ii,sz(R)) { cout << "R["< ll { ll c = R[t][l] - R[b][l] - R[t][r] + R[b][r]; return c * (c-1) / 2; }; while (Q--) { int t = read()-1; int l = read()-1; int b = read(); int r = read(); ll ans = 0; if (b - t > r - l) { rep(x, l, r) { auto tp = lower_bound(all(csx[x]), t); auto bp = lower_bound(all(csx[x]), b); while (tp != bp) { int y = *tp++; ll nr = count(y , x+1, y+1, r); ll nl = count(y , l , y+1, x); ll nt = count(t , x , y , x+1); ll nb = count(y+1, x , b , x+1); //_p("(%d,%d): %lld,%lld,%lld,%lld\n", y,x,nr,nl,nt,nb); ans += nr + nl + nt + nb; } } } else { rep(y, t, b) { auto lp = lower_bound(all(csy[y]), l); auto rp = lower_bound(all(csy[y]), r); while (lp != rp) { int x = *lp++; ll nr = count(y , x+1, y+1, r); ll nl = count(y , l , y+1, x); ll nt = count(t , x , y , x+1); ll nb = count(y+1, x , b , x+1); //_p("(%d,%d): %lld,%lld,%lld,%lld\n", y,x,nr,nl,nt,nb); ans += nr + nl + nt + nb; } } } cout << ans << endl; } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); Main(); return 0; }