#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>

typedef char                SINT8;
typedef unsigned char       UINT8;
typedef short               SINT16;
typedef unsigned short      UINT16;
typedef int                 SINT32;
typedef unsigned int        UINT32;
typedef long long           SINT64;
typedef unsigned long long  UINT64;
typedef double              DOUBLE;

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(a) ((a)>(0)?(a):-(a))
#define rep(i,a,b) for(int (i)=int(a);(i)<int(b);(i)++)

#define INF 1000000001;
#define MOD 1000000007;
#define INF64 1000000000000000001;


#define F first
#define S second

#define Pii pair<SINT32,SINT32>
#define Pll pair<SINT64,SINT64>


using namespace std;

int main() {

	SINT32 H,W;
	SINT32 cnt = 0;
	cin >> H >> W;

	vector<vector<SINT32>> DATA(H+2,vector<SINT32>(W+2));
	vector<vector<SINT32>> LOAD(H+2,vector<SINT32>(W+2));
	
	rep(i,1,H+1) {
		rep(j,1,W+1) {
			SINT8 s;
			cin >> s;
			if (s == '.') {
				DATA[i][j] = 1;
				cnt++;
			} else {
				DATA[i][j] = 0;
			}
		}
	}

	queue<Pii> qdata;
	qdata.push(Pii(1,1));
	LOAD[1][1] = 1;
	SINT32 count = 1;
	SINT32 OK = 0;

	while(qdata.size() != 0) {
		Pii p = qdata.front();
		qdata.pop();

		Pii pnext = p;

		if ((p.F == H) && (p.S == W)) {
			OK = 1;
			break;
		}

		pnext.F = p.F+1;
		pnext.S = p.S;
		if ((DATA[pnext.F][pnext.S] == 1) && (LOAD[pnext.F][pnext.S] == 0)) {
			LOAD[pnext.F][pnext.S] = LOAD[p.F][p.S] + 1;
			qdata.push(Pii(pnext.F,pnext.S));
		}

		pnext.F = p.F-1;
		pnext.S = p.S;
		if ((DATA[pnext.F][pnext.S] == 1) && (LOAD[pnext.F][pnext.S] == 0)) {
			LOAD[pnext.F][pnext.S] = LOAD[p.F][p.S] + 1;
			qdata.push(Pii(pnext.F,pnext.S));
		}

		pnext.F = p.F;
		pnext.S = p.S+1;
		if ((DATA[pnext.F][pnext.S] == 1) && (LOAD[pnext.F][pnext.S] == 0)) {
			LOAD[pnext.F][pnext.S] = LOAD[p.F][p.S] + 1;
			qdata.push(Pii(pnext.F,pnext.S));
		}

		pnext.F = p.F;
		pnext.S = p.S-1;
		if ((DATA[pnext.F][pnext.S] == 1) && (LOAD[pnext.F][pnext.S] == 0)) {
			LOAD[pnext.F][pnext.S] = LOAD[p.F][p.S] + 1;
			qdata.push(Pii(pnext.F,pnext.S));
		}

/*
		rep(i,0,H+2) {
			rep(j,0,W+2) {
				cout <<LOAD[i][j];
			}
			cout << endl;
		}
		cout << endl;
*/

	}

	if (OK == 0) {
		cout << -1 << endl;
	} else {
		cout << cnt - LOAD[H][W] << endl;
	}
	return 0;
}

//	sort(DATA.begin(),DATA.end());
//	sort(DATA.begin(),DATA.end(),std::greater<SINT32>());
//	__gcd(ANS,DATA[i]);

//	// DATA.emplace_back(BUF);	//後ろに追加

//  DATA.erase(std::unique(DATA.begin(), DATA.end()), DATA.end());	//ソート後に使用

//	DATA.insert(DATA.begin() + X, 0);	//X番目の要素に0を挿入


//	vector<vector<SINT32>> DP(N,vector<SINT32>(3));	//2次元配列

/*
	SINT32 M = 3;
	vector<pair<SINT32,SINT32>> DATA2(M);
	for (SINT32 i = 0; i < M; i++) {
		cin >> DATA2[i].first;
		cin >> DATA2[i].second;
	}
	sort(DATA2.begin(),DATA2.end());
	cout << DATA2[0].first << endl;
	cout << DATA2[0].second << endl;
*/

// 	vector<pair<int,pair<int,int>>> DATA(M);
//	cin >> DATA[i].first;
//	cin >> DATA[i].second.first;
//	cin >> DATA[i].second.second;


//		APOSI = lower_bound(ADATA.begin(),ADATA.end(),QDATA[i]) - ADATA.begin();	//key以上を探す
//		BPOSI = lower_bound(BDATA.begin(),BDATA.end(),QDATA[i]) - BDATA.begin();


/* 文字列回転
	string N;
	cin >> N;
	N = N[N.length()-1] + N.substr(0,N.length()-1);
*/

/* SET コンテナ
	set<SINT32> DATA;
	for (SINT32 i = 0; i < N; i++) {
		SINT32 buf;
		cin >> buf;
		DATA.insert(buf);
	}

	//全表示
    set<string>::iterator it; //  イテレータを用意
    for(it = DATA.begin(); it != DATA.end(); it++) {
        cout << *it << endl;
    }

	//N番目を一部表示
	set<string>::iterator it; //  イテレータを用意
	it = DATA.begin();
	rep (i,0,N) {
		it++;
	}
	cout << *it << endl;
*/

/* map
	map<string,SINT32> mp;
	SINT32 N = 0;
	SINT32 mx = 0;

	cin >> N;
	for (SINT32 i = 0; i < N; i++) {
		string s;
		cin >> s;
		mp[s]++;
	}
	for(auto it=mp.begin();it!=mp.end();it++) {
		mx=max(mx,it->second);
	}

*/

Measured memory_bytes: 256