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

use std::collections::VecDeque;

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

    let mut white = 0;

    for line in &boards {
        for ch in line {
            if *ch == '.' {
                white = white + 1;
            }
        }
    }

    let mut takes = vec![vec![0; w]; h];
    let mut visited_array = vec![vec![false; w]; h];

    if let Some(count) = bfs((0,0), (h-1, w-1), &boards, &mut takes, &mut visited_array) {
        let ans = white - count - 1;
        println!("{}", ans);
    } else {
        println!("{}", -1);
    }

}

fn bfs(start: (usize, usize), goal: (usize, usize), boards: &Vec<Vec<char>>, 
takes: &mut Vec<Vec<usize>>, visited_array: &mut Vec<Vec<bool>>) -> Option<usize> {

    let mut stack = VecDeque::new();
    let idou: Vec<(isize, isize)> = vec![(-1, 0), (0, -1), (1, 0), (0, 1)];

    stack.push_front(start);

    while !stack.is_empty() {
        let pos = stack.pop_back().unwrap();

        for ido in &idou {
           let new_x = ido.0 + pos.0 as isize;
           let new_y = ido.1 + pos.1 as isize;

           if new_x >= 0 && new_x <= goal.0 as isize && new_y >= 0 && new_y <= goal.1 as isize {
               let x = new_x as usize;
               let y = new_y as usize;

               if boards[x][y] == '.' && visited_array[x][y] == false {
                   //println!("hi");
                   visited_array[x][y] = true;
                   takes[x][y] = takes[pos.0][pos.1] + 1;
                   stack.push_front((x, y));

                   if x == goal.0 && y == goal.1 {
                       return Some(takes[x][y]);
                   }
               }
           }
        }
    }

    return None;
}



Measured memory_bytes: 4352