#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 cs(H); rep(y, H) rep(x, W) { if (B[y][x] == 'w') { R[y][x] = 1; } else { cs[y].push_back(x); } } //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(); //_p("------\n(%d,%d),(%d,%d)\n",t,l,b,r); ll ans = 0; //rep(y, t, b) rep(x, l, r) if (B[y][x] == 'c') { rep(y, t, b) { forr(x, cs[y]) if (x >= l && x < r) { 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; }