Problem:
Score: 400 points

Problem statement

We have an H \times W grid whose squares are painted black or white. The square at the i-th row from the top and the j-th column from the left is denoted as (i, j).

Snuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square (1, 1). The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square (H, W) passing only white squares.

Before Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square (1, 1) and (H, W). Also, changes of color must all be carried out before the beginning of the game.

When the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed, that is, Kenus can never reach square (H, W) regardless of how Snuke changes the color of the squares.

The color of the squares are given to you as characters s_{i, j}. If square (i, j) is initially painted by white, s_{i, j} is .; if square (i, j) is initially painted by black, s_{i, j} is #.

Constraints

H is an integer between 2 and 50 (inclusive).

W is an integer between 2 and 50 (inclusive).

s_{i, j} is . or # (1 \leq i \leq H, 1 \leq j \leq W).

s_{1, 1} and s_{H, W} are ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1}s_{1, 2}s_{1, 3} ... s_{1, W}
s_{2, 1}s_{2, 2}s_{2, 3} ... s_{2, W}
: :
s_{H, 1}s_{H, 2}s_{H, 3} ... s_{H, W}

Output

Print the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed.

Sample Input 1

3 3
..#
#..
...

Sample Output 1

2

The score 2 can be achieved by changing the color of squares as follows:

Sample Input 2

10 37
.....................................
...#...####...####..###...###...###..
..#.#..#...#.##....#...#.#...#.#...#.
..#.#..#...#.#.....#...#.#...#.#...#.
.#...#.#..##.#.....#...#.#.###.#.###.
.#####.####..#.....#...#..##....##...
.#...#.#...#.#.....#...#.#...#.#...#.
.#...#.#...#.##....#...#.#...#.#...#.
.#...#.####...####..###...###...###..
.....................................

Sample Output 2

209

Solution in Rust (1.15.1) (judge verdict: Accepted)

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);
        }
    }
}