#include #include #include #include #define repeat(i,n) for (int i = 0; (i) < int(n); ++(i)) #define repeat_from(i,m,n) for (int i = (m); (i) < int(n); ++(i)) using ll = long long; using namespace std; template struct segment_tree { // on monoid int n; vector a; function append; // associative T unit; // unit segment_tree() = default; segment_tree(int a_n, T a_unit, function a_append) { n = pow(2,ceil(log2(a_n))); a.resize(2*n-1, a_unit); unit = a_unit; append = a_append; } void point_update(int i, T z) { a[i+n-1] = z; for (i = (i+n)/2; i > 0; i /= 2) { a[i-1] = append(a[2*i-1], a[2*i]); } } T range_concat(int l, int r) { return range_concat(0, 0, n, l, r); } T range_concat(int i, int il, int ir, int l, int r) { if (l <= il and ir <= r) { return a[i]; } else if (ir <= l or r <= il) { return unit; } else { return append( range_concat(2*i+1, il, (il+ir)/2, l, r), range_concat(2*i+2, (il+ir)/2, ir, l, r)); } } }; int nc2(int n) { return n*(n-1)/2; } struct cww_t { int c, w, cw, wc, cww, wwc; }; const cww_t unit = {}; cww_t append(cww_t const & x, cww_t const & y) { cww_t z; z.c = x.c + y.c; z.w = x.w + y.w; z.cw = x.cw + x.c * y.w + y.cw; z.wc = x.wc + x.w * y.c + y.wc; z.cww = x.cww + x.cw * y.w + x.c * nc2(y.w) + y.cww; z.wwc = x.wwc + nc2(x.w) * y.c + x.w * y.wc + y.wwc; return z; } int main() { int h, w; cin >> h >> w; vector f(h); repeat (y,h) cin >> f[y]; vector > segtree_hr(h, segment_tree(w, unit, append)); vector > segtree_vt(w, segment_tree(h, unit, append)); repeat (y,h) repeat (x,w) segtree_hr[y].point_update(x, (cww_t) { (f[y][x] == 'c'), (f[y][x] == 'w'), 0, 0 }); repeat (x,w) repeat (y,h) segtree_vt[x].point_update(y, (cww_t) { (f[y][x] == 'c'), (f[y][x] == 'w'), 0, 0 }); int q; cin >> q; while (q --) { int ly, lx, ry, rx; cin >> ly >> lx >> ry >> rx; -- ly; -- lx; ll ans = 0; repeat_from (y,ly,ry) { auto it = segtree_hr[y].range_concat(lx, rx); ans += it.cww + it.wwc; } repeat_from (x,lx,rx) { auto it = segtree_vt[x].range_concat(ly, ry); ans += it.cww + it.wwc; } cout << ans << endl; } return 0; }