#include using namespace std; int h, w, q; char s[501][501], ss[501][501]; long long c[501][501], cw[501][501], cww[501][501], www[501][501]; long long C(long long n) { return n * (n - 1) / 2; } void calc() { for (int i = 0; i < h; i++) { c[i][0] = cw[i][0] = cww[i][0] = www[i][0] = 0; for (int j = 0; j < w; j++) { c[i][j + 1] = c[i][j] + (s[i][j] == 'c'); cw[i][j + 1] = cw[i][j] + (s[i][j] == 'w' ? c[i][j] : 0); cww[i][j + 1] = cww[i][j] + (s[i][j] == 'w' ? cw[i][j] : 0); www[i][j + 1] = www[i][j] + (s[i][j] == 'w'); } } } int main() { cin >> h >> w; for (int i = 0; i < h; i++) scanf("%s", s[i]); cin >> q; vector y1(q), x1(q), y2(q), x2(q); vector ans(q); for (int i = 0; i < q; i++) { scanf("%d %d %d %d", &y1[i], &x1[i], &y2[i], &x2[i]); y1[i]--; x1[i]--; y2[i]--; x2[i]--; } for (int tmp = 0; tmp < 2; tmp++) { for (int tmp2 = 0; tmp2 < 2; tmp2++) { calc(); for (int ii = 0; ii < q; ii++) { int L = x1[ii], R = x2[ii] + 1; for (int i = y1[ii]; i <= y2[ii]; i++) { long long W = www[i][R] - www[i][L]; ans[ii] += cww[i][R] - cww[i][L] - c[i][L] * C(W) - cw[i][L] * W; } } for (int i = 0; i < h; i++) reverse(s[i], s[i] + w); for (int i = 0; i < q; i++) { x1[i] = w - 1 - x1[i]; x2[i] = w - 1 - x2[i]; swap(x1[i], x2[i]); } } for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) ss[i][j] = s[j][i]; swap(s, ss); swap(h, w); for (int i = 0; i < q; i++) swap(y1[i], x1[i]), swap(y2[i], x2[i]); } for (int i = 0; i < q; i++) printf("%lld\n", ans[i]); }