#include using namespace std; using ll = long long; using vi = vector; using vvi = vector; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int h, w; cin >> h >> w; vector S(h); for (auto &s : S) cin >> s; // 切り出しによって壁になる方向 const int UP = 1; const int DOWN = 2; const int LEFT = 4; const int RIGHT = 8; // maskで指定した方向を盤面外とみなす auto key = [&](int x, int y, int mask) -> int { if (S[x][y] == '#') return 0; bool u = (mask & UP) || x == 0 || S[x-1][y] == '#'; bool d = (mask & DOWN) || x == h-1 || S[x+1][y] == '#'; bool l = (mask & LEFT) || y == 0 || S[x][y-1] == '#'; bool r = (mask & RIGHT) || y == w-1 || S[x][y+1] == '#'; // 横のカギ、縦のカギ、1マスカギ return (l && !r) || (u && !d) || (u && d && l && r); }; // 16通りの境界条件について二次元累積和 vector P(16, vvi(h+1, vi(w+1, 0))); for (int mask = 0; mask < 16; mask++) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { P[mask][i+1][j+1] = P[mask][i][j+1] + P[mask][i+1][j] - P[mask][i][j] + key(i, j, mask); } } } // 閉区間 [a,c] × [b,d] の和 auto sum = [&](int mask, int a, int b, int c, int d) -> int { if (a > c || b > d) return 0; return P[mask][c+1][d+1] - P[mask][a][d+1] - P[mask][c+1][b] + P[mask][a][b]; }; int q; cin >> q; while (q--) { int a, b, c, d; cin >> a >> b >> c >> d; --a; --b; --c; --d; // 高さ1・幅1の場合は両側を境界にする int top = (a == c ? UP | DOWN : UP); int left = (b == d ? LEFT | RIGHT : LEFT); int ans = 0; // 1. 内部 ans += sum(0, a+1, b+1, c-1, d-1); // 2. 上端・下端(角を除く) ans += sum(top, a, b+1, a, d-1); if (a < c) { ans += sum(DOWN, c, b+1, c, d-1); } // 3. 左端・右端(角を除く) ans += sum(left, a+1, b, c-1, b); if (b < d) { ans += sum(RIGHT, a+1, d, c-1, d); } // 4. 四隅 ans += key(a, b, top | left); if (b < d) { ans += key(a, d, top | RIGHT); } if (a < c) { ans += key(c, b, DOWN | left); } if (a < c && b < d) { ans += key(c, d, DOWN | RIGHT); } cout << ans << '\n'; } }