#[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)*}
    };
}

#[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, usize1) => {
        read_value!($next, usize) - 1
    };

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

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

#[allow(unused_macros)]
macro_rules! max {
    ($x:expr) => {
      $x
    };
    ($x:expr, $($xs:tt)+) => {
      max($x,max!($($xs)+))
    };
  }

#[allow(unused_macros)]
macro_rules! min {
    ($x:expr) => {
      $x
    };
    ($x:expr, $($xs:tt)+) => {
      min($x,min!($($xs)+))
    };
  }

use std::ascii::AsciiExt;
use std::cmp;
use std::collections::VecDeque;
use std::io::Write;
use std::iter::Iterator;

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

    let mut maze = wall;

    let xw = vec![1, -1, 0, 0];
    let xy = vec![0, 0, 1, -1];

    let mut depth = vec![vec![0; w]; h];
    let mut queue: VecDeque<(i32, i32)> = VecDeque::new();

    queue.push_back((0, 0));

    let mut isCrear = false;
    while let Some(v) = queue.pop_front() {
        let nowdepth = depth[v.1 as usize][v.0 as usize];
        if v.1 == (h - 1) as i32 && v.0 == (w - 1) as i32 {
            isCrear = true;
            break;
        }

        for i in 0..4 {
            let nextx = v.0 + xw[i];
            let nexty = v.1 + xy[i];

            if nextx >= 0
                && nextx < w as i32
                && nexty >= 0
                && nexty < h as i32
                && maze[nexty as usize][nextx as usize] == '.'
            {
                maze[nexty as usize][nextx as usize] = 'x';
                depth[nexty as usize][nextx as usize] = nowdepth + 1;
                queue.push_back((nextx, nexty));
            }
        }
    }

    let mut cnt = 0;
    for i in 0..w {
        for j in 0..h {
            if maze[j][i] == '.' || maze[j][i] == 'x' {
                cnt += 1;
            }
        }
    }

    if isCrear {
        println!("{}", cnt - 1 - depth[(h - 1) as usize][(w - 1) as usize]);
    } else {
        println!("-1");
    }
}


Measured memory_bytes: 4352