#include <bits/stdc++.h>
//#include <mylib.h>
using namespace std;
//cin.sync_with_stdio(false);

#define FOR(i,a,b) for(int i = (a); i < (b); ++i)
#define FOR_EQ(i,a,b) for(int i = (a); i <= (b); ++i)
#define FOR_RE(i,a,b) for(int i = (a); i > (b); --i)
#define rep(i,n) FOR(i,0,n)
#define rep_eq(i,n) FOR_EQ(i,0,n)
#define rep_re(i,n) FOR_RE(i,n,0)

#define WHITE 1//（未訪問）
#define GRAY 2//（訪問）未だに訪問していない頂点への辺を持っている。スタックに退避。
#define BLACK 3//（訪問完了）未訪問への頂点への辺を持たない
#define N 51
#define INFTY (1 << 10) //10^21
char M[N][N];
int M_cost[N][N][4];
int color[N][N];

//1個上から時計周り
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
//上右下左
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
static const int NIL = -1;

typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> Vector;
typedef vector<Vector>	DVector;
int n;
//配列の表示
void printArray(int array[], int n){
    rep(i, n){
        if(i)   cout << " ";
        cout << array[i];
    }
    cout << endl;
}

int res, h, w;

void ssp(){
    int d[N][N];
    
    FOR(i, 0, N){
        FOR(j, 0, N){
            d[i][j] = INFTY;
            color[i][j] = WHITE;
        }
    }
    
    d[1][1] = 0;
    color[1][1] = GRAY;
    
    int ux, uy, minv;
    
    for(;;){
        minv = INFTY;
        ux = -1;
        uy = -1;
        
        FOR_EQ(i, 1, h){
            FOR_EQ(j, 1, w){
                if(d[i][j] < minv && color[i][j] != BLACK){
                    ux = i;
                    uy = j;
                    minv = d[ux][uy];
                }
            }
        }
        
        if(ux == -1 && uy == -1)    break;
        
        color[ux][uy] = BLACK;
        
        rep(k, 4){
            if(color[ux + dx[k]][uy + dy[k]] != BLACK && M_cost[ux][uy][k] != INFTY){
                if(d[ux + dx[k]][uy + dy[k]] > d[ux][uy] + M_cost[ux][uy][k]){
                    d[ux + dx[k]][uy + dy[k]] = d[ux][uy] + M_cost[ux][uy][k];
                    color[ux + dx[k]][uy + dy[k]] = GRAY;
                }
            }
        }
    }
    /*
    FOR_EQ(i, 1, h){
        FOR_EQ(j, 1, w){
            cout << d[i][j] << " ";
        }
        cout << endl;
    }
    */
    res = d[h][w];
}

int main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> h >> w;
    int white_num = 0;
    FOR_EQ(i, 1, h){
        FOR_EQ(j, 1, w){
            cin >> M[i][j];
            if(M[i][j] == '.')  white_num++;
        }
    }
    
    //隣接行列への変換
    int nx, ny;
    FOR_EQ(i, 1, h){
        FOR_EQ(j, 1, w){
            rep(k, 4){
                nx = i + dx[k], ny = j + dy[k];
                if(1 <= nx && nx <= 50 && 1 <= ny && ny <= 50){
                    if(M[nx][ny] == '.'){
                        M_cost[i][j][k] = 1;
                    }else{
                        M_cost[i][j][k] = INFTY;
                    }
                }else{
                    M_cost[i][j][k] = INFTY;
                }
            }
        }
    }
    
    ssp();
    
    white_num--;
    //cout << white_num << endl;
    //cout << res << endl;
    
    if(res >= 100){
        cout << -1 << endl;
        return 0;
    }else{
        cout << white_num - res << endl;
    }
}

Measured memory_bytes: 384