#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{BinaryHeap, HashMap};

#[allow(dead_code)]
type Graph = Vec<Vec<usize>>;

fn main() {
    input! {
        h: usize, w: usize,
        mut s: [chars; h],
    }

    let mut visited = vec![vec![None; w]; h];
    use std::collections::VecDeque;
    let mut queue: VecDeque<(i64, i64, i64, i64)> = VecDeque::new();

    queue.push_back((0, 0, -1, -1));
    let dy = [0, 1, 0, -1];
    let dx = [1, 0, -1, 0];
    while !queue.is_empty() {
        let (y, x, py, px) = queue.pop_front().unwrap();
        if visited[y as usize][x as usize].is_some() {
            continue;
        }
        visited[y as usize][x as usize] = Some((py, px));
        for i in 0..4 {
            let ny = y + dy[i];
            let nx = x + dx[i];
            if ny < 0 || h <= ny as usize || nx < 0 || w <= nx as usize {
                continue;
            }
            if s[ny as usize][nx as usize] == '#' {
                continue;
            }
            if visited[ny as usize][nx as usize].is_some() {
                continue;
            }
            queue.push_back((ny, nx, y, x));
        }
    }
    if visited[h - 1][w - 1].is_none() {
        println!("-1");
        return;
    }
    let mut ans = 0;
    let mut y = (h - 1) as i64;
    let mut x = (w - 1) as i64;
    while y != -1 && x != -1 {
        // println!("{}, {}", y, x);
        s[y as usize][x as usize] = '#';
        let pre = visited[y as usize][x as usize].unwrap();
        y = pre.0;
        x = pre.1;
    }
    for i in 0..h {
        for j in 0..w {
            if s[i][j] == '.' {
                ans += 1;
            }
        }
    }

    println!("{}", ans);
}


Measured memory_bytes: 4352