#include using namespace std; #define _p(...) (void)printf(__VA_ARGS__) #define forr(x,arr) for(auto&& x:arr) #define _overload3(_1,_2,_3,name,...) name #define _rep2(i,n) _rep3(i,0,n) #define _rep3(i,a,b) for(int i=int(a);i=int(a);i--) #define rrep(...) _overload3(__VA_ARGS__,_rrep3,_rrep2,)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define bit(n) (1LL<<(n)) #define sz(x) ((int)(x).size()) #define fst first #define snd second using ll=long long;using pii=pair;using vb=vector; using vi=vector;using vvi=vector;using vvvi=vector; using vl=vector;using vvl=vector;using vvvl=vector; using vd=vector;using vvd=vector;using vvvd=vector; using vpii=vector;using vvpii=vector;using vvvpii=vector; template T read() {T t; cin >> t; return t;} int H, W; pii conv1(int y, int x, int rot) { if (rot == 0) return {y, x}; if (rot == 1) return {H-x-1, y}; if (rot == 2) return {H-y-1, W-x-1}; if (rot == 3) return {x, W-y-1}; assert(false); }; pii conv2(int y, int x, int rot) { if (rot == 0) return {y, x}; if (rot == 1) return {x, H-y-1}; if (rot == 2) return {H-y-1, W-x-1}; if (rot == 3) return {W-x-1, y}; assert(false); }; void Main() { H = read(); W = read(); vector M(H); rep(i, H) M[i] = read(); int Q = read(); vi T(Q), L(Q), B(Q), R(Q); rep(i, Q) { T[i] = read()-1; L[i] = read()-1; B[i] = read()-1; R[i] = read()-1; } vl ans(Q); int HH = H, WW = W; rep(rot, 4) { vvl c(HH, vl(WW+1)), w(HH, vl(WW+1)), cw(HH, vl(WW+1)), ww(HH, vl(WW+1)), cww(HH, vl(WW+1)); auto get = [&](int y, int x) -> char { pii yx = conv1(y, x, rot); //_p("rot: %d, (%d,%d)->(%d,%d)\n", rot, y, x, yx.fst, yx.snd); return M[yx.fst][yx.snd]; }; rep(y, HH) rep(x, WW) { c[y][x+1] = c[y][x]; w[y][x+1] = w[y][x]; cw[y][x+1] = cw[y][x]; ww[y][x+1] = ww[y][x]; cww[y][x+1] = cww[y][x]; if (get(y, x) == 'c') { c[y][x+1]++; } else { w[y][x+1]++; cw[y][x+1] += c[y][x]; ww[y][x+1] += w[y][x]; cww[y][x+1] += cw[y][x]; } } rep(q, Q) { int t = T[q]; int l = L[q]; int b = B[q]; int r = R[q]; { pii tl = conv2(t, l, rot); pii br = conv2(b, r, rot); t = min(tl.fst, br.fst); b = max(tl.fst, br.fst) + 1; l = min(tl.snd, br.snd); r = max(tl.snd, br.snd) + 1; } ll n = 0; rep(y, t, b) { n += cww[y][r] - cww[y][l] - cw[y][l] * (w[y][r] - w[y][l]) - c[y][l] * (ww[y][r] - ww[y][l] - (w[y][l] * (w[y][r] - w[y][l]))); } ans[q] += n; } swap(HH, WW); } rep(q, Q) { cout << ans[q] << endl; } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); Main(); return 0; }