#include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< pii; typedef vector vi; typedef vector vll; int H, W, Q; vector S, T; typedef tuple Query; vector q, qs[501]; int sy, sx, ey, ex; ll ans[10001], c[501][501], w[501][501], cw[501][501], cww[501][501]; void calc() { rep(ii, 2) { MEM(c, 0); MEM(w, 0); MEM(cw, 0); MEM(cww, 0); rep(y, H) { rep(x, W) { c[y][x + 1] = c[y][x] + (S[y][x] == 'c'); w[y][x + 1] = w[y][x] + (S[y][x] == 'w'); cw[y][x + 1] = cw[y][x] + (S[y][x] == 'w' ? c[y][x] : 0); cww[y][x + 1] = cww[y][x] + (S[y][x] == 'w' ? cw[y][x] : 0); } } rep(i, Q) { tie(sy, sx, ey, ex) = q[i]; FOR(y, sy, ey) { ll wa = w[y][ex] - w[y][sx]; ll ww = wa*(wa - 1) / 2; ans[i] += cww[y][ex] - cww[y][sx] - c[y][sx] * ww - cw[y][sx] * wa; } } rep(y, H)reverse(all(S[y])); rep(i, Q) { tie(sy, sx, ey, ex) = q[i]; int t = W - sx; sx = W - ex; ex = t; q[i] = tie(sy, sx, ey, ex); } } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cin >> H >> W; S.resize(H); rep(y, H)cin >> S[y]; cin >> Q; q.resize(Q); rep(i, Q) { cin >> sy >> sx >> ey >> ex; --sy; --sx; q[i] = make_tuple(sy, sx, ey, ex); } T = vector(W, string(H, ' ')); rep(y, H)rep(x, W)T[x][y] = S[y][x]; calc(); S = T; swap(W, H); rep(i, Q) { tie(sy, sx, ey, ex) = q[i]; swap(sy, sx); swap(ey, ex); q[i] = tie(sy, sx, ey, ex); } calc(); rep(i, Q)printf("%lld\n", ans[i]); }