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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

////////////////////////////////////////////////////////////////////
// This source code is for Visual Studio
////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <sstream>
#include <iterator>
#include <stack>
#include <functional>
#include <iomanip>
#include <string>
#include <cstring>
#include <deque>
#include <math.h>
#include <assert.h>

#define	numberof(a)	(sizeof(a) / sizeof(a[0]))
#define	INF		INT32_MAX
#define Rep(i,n) for(int i = 0; i < (n); ++i )

using namespace std;

typedef vector< vector<int> > MAT;
typedef pair<int, int> PINT;
typedef long long ll;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int32_t s32;
typedef int64_t s64;

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

// @param[in] G[i][j]:ノードiにつながっているj番目のノード(firstがノード番号, secondがコスト。つながっているノードだけ含まれる)
// @param[in] startNode:起点ノード番号
// @retval : 起点からノードiへの最短経路の重み、INFで初期化
vector<ll> dijkstra(vector< vector< pair<ll, ll> > > & G, ll startNode)
{
	priority_queue< pair<ll, ll> > searchPQ;	// PQの要素はこれから探索するノード(firstがコスト, secondがノード番号)
	const ll node_number = G.size();
	vector<bool> isChecked(node_number, false);	// trueのときは探索済
	vector<ll> output_dist(node_number);

	Rep(i, node_number) {
		output_dist[i] = INF;
		isChecked[i] = false;
	}

	// 起点からスタート
	output_dist[startNode] = 0;
	searchPQ.push(make_pair(0, startNode));

	while (!searchPQ.empty())
	{
		pair<ll, ll> search_node = searchPQ.top(); searchPQ.pop();
		ll u = search_node.second;// 調べるノード
		isChecked[u] = true;

		if (output_dist[u] < search_node.first * (-1)) continue;// search_node.firstは負の値にしている

		for (ll j = 0; j < G[u].size(); j++) {
			// uにつながっているノードを探索
			ll v = G[u][j].first;
			if (isChecked[v] == true) continue;
			if (output_dist[v] > output_dist[u] + G[u][j].second) {
				output_dist[v] = output_dist[u] + G[u][j].second;
				searchPQ.push(make_pair(output_dist[v] * (-1), v));//priority_queueはデフォルトで大きい値をpopしてしまうのでマイナスをかける
			}
		}
	}

	return output_dist;
}

int main()
{
	ll H = 0;
	ll W = 0;
	ll ans = 0;
	ll black = 0;

	cin >> H >> W;
	vector< vector<char> > floor(H, vector<char>(W, '.'));
	Rep(y, H) {
		Rep(x, W) {
			cin >> floor[y][x];
			if ('#' == floor[y][x]) black++;
		}
	}

	vector< vector< pair<ll, ll> > > G(H * W);// (x, y) -> node (y * W + x)
	int dx[] = { 0, 0,  -1, 1 };
	int dy[] = { 1, -1,  0, 0 };

	Rep(y, H) {
		Rep(x, W) {
			// check (x, y)
			ll node = y * W + x;

			// search around node
			Rep(k, 4) {
				ll new_x = x + dx[k];
				ll new_y = y + dy[k];
				ll new_node = new_y * W + new_x;
				if (new_x < 0 || new_x >= W) continue;
				if (new_y < 0 || new_y >= H) continue;

				if ('#' == floor[new_y][new_x]) {
					//G[node].push_back(make_pair(new_node, INF));
				}
				else if ('.' == floor[new_y][new_x]) {
					//graph.addEdge(node, new_node, 1);
					G[node].push_back(make_pair(new_node, 1));
				}
			}
		}
	}

	// solve
	// (0, 0) - (W - 1, H - 1)
	vector<ll> result = dijkstra(G, 0);
	if (result[(H - 1) * W + W - 1] == INF) {
		ans = -1;
	}
	else {
		ll black_enable = H * W - result[(H - 1) * W + W - 1] - 1;
		ans = black_enable - black;
	}

	cout << ans << endl;

	return 0;
}