import sys import numpy as np read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines H, W = map(int, readline().split()) A = np.frombuffer(read(H * (W + 1)), 'S1').reshape(H, -1)[:, :W] Q = int(readline()) nums = np.array(read().split(), np.int32) - 1 X1 = nums[::4] Y1 = nums[1::4] X2 = nums[2::4] Y2 = nums[3::4] def solve(A, X1, Y1, X2, Y2): # 上から下に向かっての cww を数える H, W = A.shape c = np.zeros((H + 1, W), np.int64) w = np.zeros((H + 1, W), np.int64) c[1:] = (A == b'c').cumsum(axis=0) w[1:] = (A == b'w').cumsum(axis=0) cw = np.zeros_like(c) ww = np.zeros_like(c) cww = np.zeros_like(c) for n in range(1, len(A)): row = A[n] is_w = row == b'w' cw[n + 1] = cw[n] + is_w * c[n] ww[n + 1] = w[n] + is_w * w[n] cww[n + 1] = cww[n] + is_w * cw[n] for x1, x2, y1, y2 in zip(X1, X2, Y1, Y2): y = slice(y1, y2 + 1, 1) x = cww[x2 + 1, y] - cww[x1, y] n = w[x2 + 1, y] - w[x1, y] x -= c[x1, y] * (n * (n - 1) // 2) x -= cw[x1, y] * (w[x2 + 1, y] - w[x1, y]) yield x.sum() answer = [0] * Q for _ in range(4): for i, x in enumerate(solve(A, X1, Y1, X2, Y2)): answer[i] += x A = A.T[::-1] H, W = A.shape X1, Y1 = H - 1 - Y1, X1 X2, Y2 = H - 1 - Y2, X2 X1, X2 = X2, X1 print('\n'.join(map(str, answer)))