import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } int H, W; string[] S; int Q; int[] A, B, C, D; void main() { try { for (; ; ) { H = readInt(); W = readInt(); S = new string[H]; foreach (x; 0 .. H) { S[x] = readToken(); } Q = readInt(); A = new int[Q]; B = new int[Q]; C = new int[Q]; D = new int[Q]; foreach (q; 0 .. Q) { A[q] = readInt() - 1; B[q] = readInt() - 1; C[q] = readInt(); D[q] = readInt(); } auto ans = new long[Q]; foreach (dir; 0 .. 2) { auto hor = new long[][][](H, W + 1, 3 + 1); foreach (x; 0 .. H) { hor[x][0][0] = 1; foreach (y; 0 .. W) { hor[x][y + 1][] = hor[x][y][]; foreach (k; 0 .. 3) { if (S[x][y] == "cww"[k]) { hor[x][y + 1][k + 1] += hor[x][y][k]; } } } } auto ver = new long[][][](H + 1, W, 3 + 1); foreach (y; 0 .. W) { ver[0][y][0] = 1; foreach (x; 0 .. H) { ver[x + 1][y][] = ver[x][y][]; foreach (k; 0 .. 3) { if (S[x][y] == "cww"[k]) { ver[x + 1][y][k + 1] += ver[x][y][k]; } } } } debug { writeln("hor = ", hor); writeln("ver = ", ver); } foreach (q; 0 .. Q) { foreach (x; A[q] .. C[q]) { const long w = (D[q] - B[q]) - (hor[x][D[q]][1] - hor[x][B[q]][1]); long tmp = hor[x][D[q]][3]; tmp -= hor[x][B[q]][3]; tmp -= hor[x][B[q]][2] * w; tmp -= hor[x][B[q]][1] * w * (w - 1) / 2; debug { writeln("x = ", x, ": ", tmp); } ans[q] += tmp; } foreach (y; B[q] .. D[q]) { const long w = (C[q] - A[q]) - (ver[C[q]][y][1] - ver[A[q]][y][1]); long tmp = ver[C[q]][y][3]; tmp -= ver[A[q]][y][3]; tmp -= ver[A[q]][y][2] * w; tmp -= ver[A[q]][y][1] * w * (w - 1) / 2; debug { writeln("y = ", y, ": ", tmp); } ans[q] += tmp; } } S.reverse; foreach (x; 0 .. H) { S[x] = S[x].dup.to!(char[]).reverse.to!string; } foreach (q; 0 .. Q) { swap(A[q], C[q]); swap(B[q], D[q]); A[q] = H - A[q]; C[q] = H - C[q]; B[q] = W - B[q]; D[q] = W - D[q]; } } foreach (q; 0 .. Q) { writeln(ans[q]); } } } catch (EOFException e) { } }