use fio::*; fn main() { let [h, w] = read_tuple::(); let mut s = vec![]; for _ in 0..h { s.push(read_line().chars().collect::>()); } let mut dp = vec![vec![vec![vec![vec![vec![0; w + 1]; h + 1]; 2]; 2]; 2]; 2]; for lb in 0..=1 { for rb in 0..=1 { for ub in 0..=1 { for db in 0..=1 { for r in 0..h { for c in 0..w { let lc = if lb == 1 || c == 0 { '#' } else { s[r][c - 1] }; let rc = if rb == 1 || c == w - 1 { '#' } else { s[r][c + 1] }; let uc = if ub == 1 || r == 0 { '#' } else { s[r - 1][c] }; let dc = if db == 1 || r == h - 1 { '#' } else { s[r + 1][c] }; let mc = s[r][c]; let hor = mc == '.' && lc == '#' && rc == '.'; let ver = mc == '.' && uc == '#' && dc == '.'; let sin = mc == '.' && lc == '#' && rc == '#' && uc == '#' && dc == '#'; let dp = &mut dp[lb][rb][ub][db]; if hor || ver || sin { dp[r + 1][c + 1] = 1; } dp[r + 1][c + 1] += dp[r][c + 1] + dp[r + 1][c] - dp[r][c]; } } } } } } let q = read::(); let getsum = |a: &Vec>, x1: usize, y1: usize, x2: usize, y2: usize| -> i32 { a[x2][y2] - a[x1][y2] - a[x2][y1] + a[x1][y1] }; for _ in 0..q { let [x1, y1, x2, y2] = read_tuple::(); let [x1, y1, x2, y2] = [x1 - 1, y1 - 1, x2, y2]; let udx1x2 = if x1 + 1 == x2 { vec![(1, 1, x1, x2)] } else { vec![ (1, 0, x1, x1 + 1), (0, 0, x1 + 1, x2 - 1), (0, 1, x2 - 1, x2), ] }; let lry1y2 = if y1 + 1 == y2 { vec![(1, 1, y1, y2)] } else { vec![ (1, 0, y1, y1 + 1), (0, 0, y1 + 1, y2 - 1), (0, 1, y2 - 1, y2), ] }; let mut ans = 0; for (u, d, x1, x2) in udx1x2 { for (l, r, y1, y2) in lry1y2.iter().copied() { ans += getsum(&dp[l][r][u][d], x1, y1, x2, y2); } } println!("{ans}") } } mod fio { use std::{ cell::RefCell, convert::TryInto, fmt::Debug, io::{BufRead, BufWriter, StdinLock, StdoutLock, stdin, stdout}, str::FromStr, }; thread_local! { pub static STDIN: RefCell> = RefCell::new(stdin().lock()); pub static STDOUT: RefCell>> = RefCell::new(BufWriter::new(stdout().lock())); } #[allow(dead_code)] pub fn read() -> T where ::Err: Debug, { read_line().parse().unwrap() } /// If there is a performance concern, use direct /// `read_line().spilt_ascii_whitespace().map(|x| x.parse().unwrap())` #[allow(dead_code)] pub fn read_vec() -> Vec where ::Err: Debug, { read_line() .split_ascii_whitespace() .map(|x| x.parse().unwrap()) .collect() } /// If there is a performance concern, use direct /// `read_line().spilt_ascii_whitespace().map(|x| x.parse().unwrap())` #[allow(dead_code)] pub fn read_tuple() -> [T; N] where T: FromStr + Debug, ::Err: Debug, { read_vec::().try_into().unwrap() } /// whitespace at the end of the line is ignored pub fn read_line() -> String { let mut s = String::new(); STDIN.with(|cell| { cell.borrow_mut().read_line(&mut s).unwrap(); }); String::from_str(s.trim_end()).unwrap() } } #[macro_export] macro_rules! print { ($($t:tt)*) => { fio::STDOUT.with(|cell|{ use std::io::Write; write!(cell.borrow_mut(), $($t)*).unwrap() })}; } #[macro_export] macro_rules! println { ($($t:tt)*) => { fio::STDOUT.with(|cell| { use std::io::Write; writeln!(cell.borrow_mut(), $($t)*).unwrap() }) }; } #[macro_export] macro_rules! flush { () => { fio::STDOUT.with(|cell| { use std::io::Write; cell.borrow_mut().flush().unwrap() }); }; }