using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.IO; class Iroha { public Iroha() { } public static int Main() { new Iroha().calc(); return 0; } Scanner cin; int H, W; string[] S; int[,] board; void calc() { cin = new Scanner(); H = cin.nextInt(); W = cin.nextInt(); S = new string[H]; board = new int[H, W]; for (int i = 0; i < H; i++) { S[i] = cin.next(); for (int j = 0; j < W; j++) { if (S[i][j] == 'w') board[i, j] = 1; } } int Q = cin.nextInt(); int[] a = new int[Q]; int[] b = new int[Q]; int[] c = new int[Q]; int[] d = new int[Q]; for (int i = 0; i < Q; i++) { a[i] = cin.nextInt() - 1; b[i] = cin.nextInt() - 1; c[i] = cin.nextInt(); d[i] = cin.nextInt(); } precalc(); long[] ans = new long[Q]; for (int i = 0; i < Q; i++) { ans[i] += calc(a[i], b[i], c[i], d[i]); } int[,] board2 = new int[W, H]; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { board2[j, i] = board[i, j]; } } board = board2; swap(ref H, ref W); precalc(); for (int i = 0; i < Q; i++) { ans[i] += calc(b[i], a[i], d[i], c[i]); Console.WriteLine(ans[i]); } } long[,] dpc; long[,] dpcw; long[,] dpcww; long[,] dpw; long[,] dpww; long[,] dpwwc; long[,] dpwc; void precalc() { dpc = new long[H, W + 1]; dpcw = new long[H, W + 1]; dpcww = new long[H, W + 1]; dpw = new long[H, W + 1]; dpww = new long[H, W + 1]; dpwwc = new long[H, W + 1]; dpwc = new long[H, W + 1]; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { dpc[i, j + 1] = dpc[i, j]; dpcw[i, j + 1] = dpcw[i, j]; dpcww[i, j + 1] = dpcww[i, j]; dpw[i, j + 1] = dpw[i, j]; dpww[i, j + 1] = dpww[i, j]; dpwwc[i, j + 1] = dpwwc[i, j]; dpwc[i, j + 1] = dpwc[i, j]; //c if (board[i,j] == 0) { dpc[i, j + 1]++; dpwwc[i, j + 1] += dpww[i, j]; dpwc[i, j + 1] += dpw[i, j]; } //w else { dpcw[i, j + 1] += dpc[i, j]; dpcww[i, j + 1] += dpcw[i, j]; dpw[i, j + 1]++; dpww[i, j + 1] += dpw[i, j]; } } } } long calc(int a, int b, int c, int d) { long ans = 0; for (int h = a; h < c; h++) { ans += dpwwc[h, d] - dpwwc[h, b] - dpww[h, b] * (dpc[h, d] - dpc[h, b]) - dpw[h, b] * (dpwc[h, d] - dpwc[h, b] - (dpw[h, b] * (dpc[h, d] - dpc[h, b]))); ans += dpcww[h, d] - dpcww[h, b] - dpcw[h, b] * (dpw[h, d] - dpw[h, b]) - dpc[h, b] * (dpww[h, d] - dpww[h, b] - (dpw[h, b] * (dpw[h, d] - dpw[h, b]))); } return ans; } //swap void swap(ref T a, ref T b) { T c = a; a = b; b = c; } } class Scanner { string[] s; int i; char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string next() { if (i < s.Length) return s[i++]; string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); i = 0; return next(); } public int nextInt() { return int.Parse(next()); } public long nextLong() { return long.Parse(next()); } public double nextDouble() { return double.Parse(next()); } }