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)

#include <bits/stdc++.h>  // ver2.1.9
#define int long long
#define endl "\n"
#define ALL(v) (v).begin(),(v).end()
#define Vi vector<int>
#define Pii pair<int,int>
#define Tiii tuple<int,int,int>
#define PQi priority_queue<int>
#define PQir priority_queue<int,vector<int>,greater<int>>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define cYES cout<<"YES"<<endl
#define cNO cout<<"NO"<<endl
#define cYes cout<<"Yes"<<endl
#define cNo cout<<"No"<<endl
#define sortr(v) sort(v,greater<>())
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repeq(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(int i=a;i>b;i--)
#define repreq(i,a,b) for(int i=a;i>=b;i--)
#define leng(n) (int)(log10(n)+1)
#define dem(a,b) ((a+b-1)/(b))
#define Vout(a) rep(lZ,0,a.size()-1)cout<<a[lZ]<<' ';cout<<a.back()<<endl
#define VVout(a) rep(lW,0,a.size()){Vout(a[lW]);}
#define INF 9000000000000000000  //  9.0 * 10^18
#define MOD 1000000007  //  10^9 + 7
using namespace std;
 
int gcd(int a,int b){return b?gcd(b,a%b):a;}  //  最大公約数gcd
int lcm(int a,int b){return a/gcd(a,b)*b;}  //  最小公倍数lcm
void uniq(Vi &v){sort(ALL(v)); v.erase(unique(ALL(v)),v.end());}  //  uniqのやつ
 
int mypow(int x, int n, int m){  //  累乗x^n(mod m)  O(log(n))
if(n==0)return 1;if(n%2==0)return mypow(x*x%m,n/2,m);else return x*mypow(x,n-1,m)%m;}
 
signed main() {
  cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(12);
  
  int h,w,bl=0,minn;
  cin >> h >> w;
  minn = min(h,w);
  vector<string> maze(h);
  rep(i,0,h) cin >> maze[i];
  
  Pii S=mp(0,0),G=mp(h-1,w-1);
  
  rep(i,0,h){
    rep(j,0,w){
      if(maze[i][j]=='#') bl++;
    }
  }
  int wh = h*w - bl;
  
  
  if(bl < minn) {cout << (h-1)*(w-1)-bl << endl;return 0;}
  
  // ここから
  
  vector<string> maze2 = maze;
  stack<Pii> st2;
  st2.push(S);
  int x2,y2;
  bool ok=false;
  
  while(true){
  while(!st2.empty()){
    x2 = st2.top().first;
    y2 = st2.top().second;
    st2.pop();
    if(mp(x2,y2)==G) {ok = true;break;}
    
    if(y2+1 < w) {if(maze2[x2][y2+1]=='.') st2.push(mp(x2,y2+1));}
    if(y2-1 >= 0) {if(maze2[x2][y2-1]=='.') st2.push(mp(x2,y2-1));}
    if(x2+1 < h) {if(maze2[x2+1][y2]=='.') st2.push(mp(x2+1,y2));}
    if(x2-1 >= 0) {if(maze2[x2-1][y2]=='.') st2.push(mp(x2-1,y2));}
    maze2[x2][y2] = '#';
    
  }
  if(ok) break;
  cout << -1 << endl;
  return 0;
  }
  
  //　　ここまで
  
  queue<Pii> st;
  st.push(S);
  int x,y,ct=0,now;
  
  while(!st.empty()){
    now = st.size();
    if(now >= 10000) {cout << (h-1)*(w-1)-bl << endl;return 0;}
    ct++;
    rep(i,0,now){
      x = st.front().first;
      y = st.front().second;
      st.pop();
      if(mp(x,y)==G) {cout << wh-ct << endl;return 0;}
    
      if(y+1 < w) {if(maze[x][y+1]=='.') st.push(mp(x,y+1));}
      if(y-1 >= 0) {if(maze[x][y-1]=='.') st.push(mp(x,y-1));}
      if(x+1 < h) {if(maze[x+1][y]=='.') st.push(mp(x+1,y));}
      if(x-1 >= 0) {if(maze[x-1][y]=='.') st.push(mp(x-1,y));}
 
      //VVout(maze);
      maze[x][y] = '#';
    }
  }
  
  
  cout << -1 << endl;
  return 0;
}