#include "bits/stdc++.h" using namespace std; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; typedef vector vi; typedef pair pii; typedef vector > vpii; typedef long long ll; template static void amin(T &x, U y) { if(y < x) x = y; } template static void amax(T &x, U y) { if(x < y) x = y; } struct Query { int yL; int xL; int yR; int xR; }; int main() { int H; int W; while(~scanf("%d%d", &H, &W)) { vector> S(H, vector(W)); rep(i, H) { char buf[501]; scanf("%s", buf); rep(j, W) S[i][j] = buf[j]; } auto calc = [](int n) { return (ll)n * (n - 1) / 2; }; int Q; scanf("%d", &Q); vector queries(Q); rep(i, Q) { int yL; int xL; int yR; int xR; scanf("%d%d%d%d", &yL, &xL, &yR, &xR), -- yL, -- xL; queries[i] = Query{ yL,xL,yR,xR }; } vector ans(Q, 0); rep(rot, 2) { vector sum(H, vi(W + 1)); rep(i, H) rep(j, W) sum[i][j + 1] = sum[i][j] + (S[i][j] == 'w'); vector> sumL(H, vector(W + 1)), sumLR(H, vector(W + 1)); rep(i, H) rep(j, W) { int wL = sum[i][j], cL = j - wL; int wR = sum[i][W] - sum[i][j + 1], cR = (W - (j + 1)) - wR; sumL[i][j + 1] = sumL[i][j] + (S[i][j] == 'w' ? cL : 0); sumLR[i][j + 1] = sumLR[i][j] + (S[i][j] == 'w' ? (ll)wL * cR + (ll)cL * wR : 0); } rep(qi, Q) { Query q = queries[qi]; ll total = 0; reu(i, q.yL, q.yR) { int n = q.xR - q.xL; int w = sum[i][q.xR] - sum[i][q.xL], c = n - w; int wL = sum[i][q.xL], cL = q.xL - wL; int wR = sum[i][W] - sum[i][q.xR], cR = (W - q.xR) - wR; ll ww = calc(w), cc = calc(c); ll cw = (sumL[i][q.xR] - sumL[i][q.xL]) - (ll)w * cL; ll wc = calc(n) - ww - cc - cw; total += sumLR[i][q.xR] - sumLR[i][q.xL]; total -= (ll)cL * w * wR + (ll)wL * w * cR; total -= cw * wR + wL * wc; total -= cL * ww + ww * cR; } ans[qi] += total; } vector> nS(W, vector(H)); rep(i, H) rep(j, W) nS[j][i] = S[i][j]; for(auto &q : queries) { swap(q.yL, q.xL); swap(q.yR, q.xR); } swap(H, W); S.swap(nS); } for(int i = 0; i < Q; ++ i) printf("%lld\n", ans[i]); } return 0; }