#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; } template struct RectangeSum { vector > sum; template void init(int h, int w, U &a) { sum.assign(h + 1, vector(w + 1)); rer(y, 0, h) rer(x, 0, w) sum[y][x] = (y == 0 || x == 0) ? 0 : sum[y - 1][x] + sum[y][x - 1] - sum[y - 1][x - 1] + a[y - 1][x - 1]; } //[l, r), [t, b) inline T get(int t, int l, int b, int r) const { return sum[b][r] - sum[b][l] - sum[t][r] + sum[t][l]; } }; class SolveForHorizontalPatterns { public: void init(const vector> &S) { int H = (int)S.size(), W = (int)S[0].size(); myW = W; //sum.assign(H, vi(W + 1)); rep(i, H) sum[i][0] = 0; rep(i, H) rep(j, W) sum[i][j + 1] = sum[i][j] + (S[i][j] == 'w'); //sumL.assign(H, vector(W + 1)); //sumLR.assign(H, vector(W + 1)); rep(i, H) sumLR[i][0] = sumL[i][0] = 0; 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); } } ll query(int yL, int xL, int yR, int xR) const { int W = myW; ll total = 0; reu(i, yL, yR) { int n = xR - xL; int w = sum[i][xR] - sum[i][xL], c = n - w; int wL = sum[i][xL], cL = xL - wL; int wR = sum[i][W] - sum[i][xR], cR = (W - xR) - wR; ll ww = calc(w), cc = calc(c); ll cw = (sumL[i][xR] - sumL[i][xL]) - (ll)w * cL; ll wc = calc(n) - ww - cc - cw; total += sumLR[i][xR] - sumLR[i][xL]; total -= (ll)cL * w * wR + (ll)wL * w * cR; total -= cw * wR + wL * wc; total -= cL * ww + ww * cR; } return total; } private: static ll calc(int n) { return (ll)n * (n - 1) / 2; } int myW; int sum[500][501]; ll sumL[500][501], sumLR[500][501]; }; int main() { int H; int W; while(~scanf("%d%d", &H, &W)) { vector> S(H, vector(W)), transS(W, vector(H)); rep(i, H) { char buf[501]; scanf("%s", buf); rep(j, W) transS[j][i] = S[i][j] = buf[j]; } SolveForHorizontalPatterns solverH, solverV; solverH.init(S); solverV.init(transS); int Q; scanf("%d", &Q); rep(i, Q) { int yL; int xL; int yR; int xR; scanf("%d%d%d%d", &yL, &xL, &yR, &xR), -- yL, -- xL; ll ans = 0; ans += solverH.query(yL, xL, yR, xR); ans += solverV.query(xL, yL, xR, yR); printf("%lld\n", ans); } } return 0; }