use std::io::{self, BufRead, BufWriter, Write};
use std::collections::HashSet;
use std::collections::VecDeque;

struct Board<T>(Vec<Vec<T>>);

impl<T> Board<T> {
    fn new(v: Vec<Vec<T>>) -> Self {
        Board(v)
    }

    fn get(&self, i: i32, j: i32) -> &T {
        let i0 = i as usize - 1;
        let j0 = j as usize - 1;
        assert!(1 <= i);
        assert!(i0 < self.0.len());
        assert!(1 <= j);
        assert!(j0 < self.0[0].len());
        &self.0[i0][j0]
    }

    fn set(&mut self, i: i32, j: i32, item: T) {
        let i0 = i as usize - 1;
        let j0 = j as usize - 1;
        assert!(1 <= i);
        assert!(i0 < self.0.len());
        assert!(1 <= j);
        assert!(j0 < self.0[0].len());
        self.0[i0][j0] = item;
    }

    fn h(&self) -> i32 {
        self.0.len() as i32
    }

    fn w(&self) -> i32 {
        self.0[0].len() as i32
    }
}

fn solve<R, W>(reader: R, mut writer: W)
where
    R: BufRead,
    W: Write,
{
    let mut lines = reader.lines();
    let a_line = lines.next().unwrap().unwrap();
    let mut splited = a_line.trim().split_whitespace();
    let _h: u32 = splited.next().unwrap().parse().unwrap();
    let _w: u32 = splited.next().unwrap().parse().unwrap();
    let raw_board: Vec<Vec<bool>> = lines
        .map(|s| {
            s.unwrap().trim().chars()
                .map(|c| if c == '#' { true } else { false })
                .collect()
        }).collect();

    let ans = solve_internal(&Board::new(raw_board));

    writeln!(&mut writer, "{}", ans).unwrap();
}

fn solve_internal(board: &Board<bool>) -> i32 {
    let mut q: VecDeque<(i32, i32)> = VecDeque::new();
    q.push_back((1, 1));

    let mut done: HashSet<(i32, i32)> = HashSet::new();
    done.insert((1, 1));
    let mut dist: Board<i32> = Board::new(
        vec![vec![-1; board.w() as usize]; board.h() as usize]
    );

    'outer: while ! q.is_empty() {
        let x = q.pop_front().unwrap();

        let ys: Vec<(i32, i32)> = vec![
            (x.0, x.1 + 1),
            (x.0 + 1, x.1),
            (x.0, x.1 - 1),
            (x.0 - 1, x.1),
        ];

        let ys: Vec<_> = ys.iter().filter(|&&(i, j)| {
            1 <= i && i <= board.h() &&
            1 <= j && j <= board.w() &&
            ! done.contains(&(i, j)) &&
            ! board.get(i, j)
        }).collect();

        for &(i, j) in ys {
            let n = *dist.get(x.0, x.1);
            dist.set(i, j, n + 1);
            if i == board.h() && j == board.w() {
                break 'outer;
            }
            q.push_back((i, j));
            done.insert((i, j));
        }
    }

    let d = *dist.get(board.h(), board.w());
    if d < 0 {
        return -1;
    }

    let s: i32 = board.0.iter().map(|v| {
        let ret: i32 = v.iter().map(|x| if *x { 1 } else { 0 } ).sum();
        ret
    }).sum();

    board.h() * board.w() - (d + 2) - s
}

fn main() {
    let stdio = io::stdin();
    let input = stdio.lock();

    let stdout = io::stdout();
    let output = BufWriter::new(stdout.lock());

    solve(input, output);
}

#[cfg(test)]
mod tests {
    use super::*;

    const TESTS: &'static [(&'static [u8], &'static [u8])] = &[
//         (
//             br"3 3
// ..#
// #..
// ...", br"2
// ",
//         ),
//         (
//             br"10 37
// .....................................
// ...#...####...####..###...###...###..
// ..#.#..#...#.##....#...#.#...#.#...#.
// ..#.#..#...#.#.....#...#.#...#.#...#.
// .#...#.#..##.#.....#...#.#.###.#.###.
// .#####.####..#.....#...#..##....##...
// .#...#.#...#.#.....#...#.#...#.#...#.
// .#...#.#...#.##....#...#.#...#.#...#.
// .#...#.####...####..###...###...###..
// .....................................", br"209
// ",
//         ),
//         (
//             br"2 2
// .#
// #.
// ", br"-1
// ",
//        ),
        (
            br"50 50
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
", br"2401
",
        ),
    ];

    #[test]
    fn test_d() {
        for &(input, expected) in TESTS {
            let mut output = Vec::new();
            solve(input, &mut output);
            let expected_str = String::from_utf8(expected.to_vec()).unwrap();
            let output_str = String::from_utf8(output).unwrap();
            assert_eq!(expected_str, output_str);
        }
    }
}


Measured memory_bytes: 4352