#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
/* #include <regex> */

using namespace std;

/* g++ -g -std=c++11 */

/* freopen("input.txt", "rt", stdin); */
/* freopen("output.txt", "wt", stdout); */

#define ALL(c)          (c).begin(), (c).end()
#define ALLR(c)         (c).rbegin(), (c).rend()
#define FOR(i,a,b)      for (int i=(a); i < (b); ++i)
#define FORR(i,a,b)     for (int i=(a); i > (b); --i)
#define FOR_ALL(i,c)    for (__typeof((c).begin()) i=(c).begin();   \
                             i != (c).end(); ++i)
#define FOR_ALLR(i,c)   for (__typeof((c).rbegin()) i=(c).rbegin(); \
                             i != (c).rend(); ++i)
#define SZ(array)       (sizeof(array)/sizeof(array[0]))
#define lc(x)           (x<<1)     /* 2*x */
#define rc(x)           (x<<1 | 1) /* 2*x+1 */
#define lowbit(x)       (x & (-x)) /* 0b10100 -> 0b100 */

typedef long long       LL;
typedef map<int,int>    MII;
typedef pair<int,int>   PII;
typedef set<int>        SI;
typedef vector<bool>    VB;
typedef vector<double>  VD;
typedef vector<int>     VI;
typedef vector<string>  VS;

/* check if a key is in container C */
template <class C>
inline bool in_(const typename C::key_type& k, const C& A)
{ return A.find(k) != A.end(); }
inline bool in_(const string& s, const string& S)
{ return S.find(s) != string::npos; }

const int dx[] = { 1,-1,0,0 };
const int dy[] = { 0,0,-1,1 };
const int inf = 1e9;
const int maxn = 60;
char G[maxn][maxn];
int H,W, dist[maxn][maxn], pre[maxn][maxn][2], path[maxn][maxn];

void dfs(const int i, const int j)
{
    FOR(d,0,4) {
        const int x = i+dx[d];
        const int y = j+dy[d];
        if (0 < x && x < H+1 && 0 < y && y < W+1) {
            if (G[x][y] == '.') {
                const int d = dist[i][j] + 1;
                if (d < dist[x][y]) {
                    dist[x][y] = d;
                    pre[x][y][0] = i;
                    pre[x][y][1] = j;
                    dfs(x,y);
                }
            }
        }
    }
}

int main()
{
    while (cin >> H >> W) {
        FOR(i,1,H+1) FOR(j,1,W+1) {
            cin >> G[i][j];
            dist[i][j] = inf;
            path[i][j] = 0;
        }
        dist[1][1] = 0;
        dfs(1,1);
        if (dist[H][W] == inf)
            cout << -1 << endl;
        else {
            int i = H, j = W;
            while (i && j) {
                path[i][j] = 1;
                const int x = pre[i][j][0];
                const int y = pre[i][j][1];
                i = x; j = y;
            }
            int ans = 0;
            FOR(i,1,H+1) FOR(j,1,W+1) if (G[i][j] == '.') {
                if (i == 1 && j == 1)
                    continue;
                if (i == H && j == W)
                    continue;
                if (path[i][j])
                    continue;
                ans++;
            }
            cout << ans << endl;
        }
    }
}


Measured memory_bytes: 384