#[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),*) => {
        println!(concat!($(stringify!($a), "={:?} "),*), $($a),*);
    }
}

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

fn search(ss: &Vec<Vec<char>>, i: i64, j: i64, scores: &mut Vec<Vec<i64>>, score: i64) {
    let h = ss.len() as i64;
    let w = ss[0].len() as i64;
    let n_score = score + 1;

    for &(dx, dy) in [(1, 0), (-1, 0), (0, 1), (0, -1)].iter() {
        let nx = i + dx;
        let ny = j + dy;
        if nx < 0 || nx >= h { continue };
        if ny < 0 || ny >= w { continue };
        let nx = nx as usize;
        let ny = ny as usize;
        if ss[nx][ny] == '#' { continue };

        if n_score >= scores[nx][ny] && scores[nx][ny] != -1 {
            continue;
        }
        scores[nx][ny] = n_score;
        search(ss, nx as i64, ny as i64, scores, n_score);
    }
}


fn main() {
    let stdin = std::io::stdin();
    let mut buf = String::new();
    stdin.read_line(&mut buf).unwrap();
    let hws = buf.split_whitespace().map(|v| v.parse().unwrap()).collect::<Vec<usize>>();

    let h = hws[0];
    let w = hws[1];

    let mut ss = vec![];
    for _ in 0..h {
        let mut buf = String::new();
        stdin.read_line(&mut buf).unwrap();
        let buf = buf.trim_right();
        let row = buf.chars().collect::<Vec<char>>();
        ss.push(row);
    }

    let mut scores = vec![vec![-1i64; w]; h];
    scores[0][0] = 0;
    search(&ss, 0, 0, &mut scores, 0);

    let score = scores[h-1][w-1];
    let mut cnt = 0;
    for row in ss {
        for c in row {
            if c == '.' {
                cnt += 1;
            }
        }
    }
    if score == -1 {
        println!("{}", -1);
    } else {
        println!("{}", cnt-score-1);
    }
}


Measured memory_bytes: 4476