use std::io::*;
use std::str::FromStr;
#[allow(unused_imports)]
use std::collections::*;
#[allow(unused_imports)]
use std::cmp::{min, max};

#[allow(unused_macros)]
macro_rules! dump (
    ($($arg:tt)*) => { {
        if cfg!(local) {
            let r = writeln!(&mut ::std::io::stderr(), $($arg)*);
            r.expect("failed printing to stderr");
        }
    } }
);

#[allow(dead_code)]
struct Chrono {
    st: std::time::SystemTime,
}

#[allow(dead_code)]
impl Chrono {
    fn new() -> Chrono {
        Chrono { st: std::time::SystemTime::now() }
    }

    fn elapsed_ms(&self) -> i64 {
        let elapsed = self.st.elapsed().unwrap();
        elapsed.as_secs() as i64 * 1000 + elapsed.subsec_nanos() as i64 / 1_000_000
    }

    fn reset(&mut self) {
        self.st = std::time::SystemTime::now();
    }
}

struct Scanner<R: Read> {
    reader: R,
    buffer: String,
}

#[allow(dead_code)]
impl<R: Read> Scanner<R> {
    pub fn new(reader: R) -> Scanner<R> {
        Scanner { reader: reader, buffer: String::new() }
    }

    pub fn line(&mut self) -> String {
        self.buffer = self.reader.by_ref().bytes().map(|c| c.unwrap() as char)
            .skip_while(|&c| c == '\n' || c == '\r')
            .take_while(|&c| !(c == '\n' || c == '\r'))
            .collect::<String>();
        self.buffer.clone()
    }

    pub fn read_buffer(&mut self) {
        self.buffer = self.reader.by_ref().bytes().map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
    }

    pub fn safe_read<T: FromStr>(&mut self) -> Option<T> {
        self.read_buffer();
        if self.buffer.is_empty() {
            None
        } else {
            self.buffer.parse::<T>().ok()
        }
    }

    pub fn read<T: FromStr>(&mut self) -> T {
        if let Some(s) = self.safe_read() {
            s
        } else {
            if cfg!(local) {
                writeln!(std::io::stderr(), "Terminated with EOF").unwrap();
            }
            std::process::exit(0);
        }
    }

    pub fn vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
        (0..len).map(|_| self.read()).collect()
    }

    pub fn mat<T: FromStr>(&mut self, row: usize, col: usize) -> Vec<Vec<T>> {
        (0..row).map(|_| self.vec(col)).collect()
    }
}

trait Joinable {
    fn join(self, sep: &str) -> String;
}

impl<U: ToString, T: Iterator<Item=U>> Joinable for T {
    fn join(self, sep: &str) -> String {
        self.map(|x| x.to_string()).collect::<Vec<_>>().join(sep)
    }
}

fn main() {
    std::thread::Builder::new()
        .stack_size(104_857_600)
        .spawn(solve)
        .unwrap()
        .join()
        .unwrap();
}

fn grid_bfs(g: &Vec<Vec<char>>, start: char, wall: char) -> Vec<Vec<u32>> {
    let INF: u32 = u32::max_value();
    let (h, w) = (g.len(), g[0].len());
    let mut dist = vec![vec![INF; w]; h];
    let mut queue = VecDeque::new();
    for (i, row) in g.iter().enumerate() {
        for (j, cell) in row.iter().enumerate() {
            if *cell == start {
                queue.push_back((i, j));
                dist[i][j] = 0;
            }
        }
    }

    while let Some((ci, cj)) = queue.pop_front() {
        for &(di, dj) in [(1, 0), (0, 1), (-1, 0), (0, -1)].iter() {
            let ni = ci as isize + di;
            let nj = cj as isize + dj;
            if 0 <= ni && ni < h as isize && 0 <= nj && nj < w as isize {
                let (ni, nj) = (ni as usize, nj as usize);
                if dist[ni][nj] == INF && g[ni][nj] != wall {
                    dist[ni][nj] = dist[ci][cj] + 1;
                    queue.push_back((ni, nj));
                }
            }
        }
    }

    dist
}

fn solve() {
    let cin = stdin();
    let cin = cin.lock();
    let mut sc = Scanner::new(cin);

    loop {
        let INF: u32 = u32::max_value();
        let h = sc.read();
        let w = sc.read();
        let mut g: Vec<Vec<char>> = (0..h).map(|_| {
            let row: String = sc.read();
            assert_eq!(row.len(), w);
            row.chars().collect()
        }).collect();
        g[0][0] = '@';
        let dist = grid_bfs(&g, '@', '#');
        if dist[h-1][w-1] == INF {
            println!("{}", -1);
        } else {
            let mut white = 0;
            for row in g.iter() {
                for &cell in row.iter() {
                    if cell != '#' {
                        white += 1;
                    }
                }
            }
            println!("{}", white - dist[h-1][w-1] - 1);
        };
    }
}


Measured memory_bytes: 8572