// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)

#define rep3(i, s, n) for (int i = s; i > (int)(n); i--)

#define all(obj) obj.begin(), obj.end()

#define db(x) cerr << #x << ":" << x << " "

#define dbl(x) cerr << #x << ":" << x << "\n"

#define dbv(vec) cerr << #vec << ":"; for (auto e : vec) cerr << e << ", "; cout << "\n"

#define dbvv(vv) cerr << #vv << ":\n"; for (auto vec : vv) { for (auto e : vec) cerr << e << ", "; cerr << "\n"; }

#define dbm(map) cerr << #map << ":"; for (auto p : map) cerr << "(" << p.first << ", " << p.second << "), "; cerr << "\n";

#define YN(f) cout << (f ? "YES" : "NO") << "\n"

#define Yn(f) cout << (f ? "Yes" : "No") << "\n"

#define yn(f) cout << (f ? "yes" : "no") << "\n"

using ll = long long;

using vi = vector<int>; using vl = vector<ll>; using vs = vector<string>; using vb = vector<bool>;

using vvi = vector<vi>; using vvl = vector<vl>; using vvb = vector<vb>;

using pii = pair<int, int>; using pll = pair<ll, ll>;

using vpii = vector<pii>; using vpll = vector<pll>;

using ti3 = tuple<int, int, int>; using tl3 = tuple<ll, ll, ll>;

using mii = map<int, int>; using mil = map<int, ll>; using mli = map<ll, int>; using mll = map<ll, ll>;



int main() {

	int H, W; cin >> H >> W;

	vvb g(H, vb(W));

	vvi d(H, vi(W, -1));

	int white = 0;

	rep(i, H) {

		string s; cin >> s;		

		rep(j, W)	{

			if (s[j] == '.') {

				g[i][j] = true;

				white++;

			}

		}

	}

	dbvv(g); dbl(white);

	queue<pii> q;

	pii s(0, 0); q.push(s); d[0][0] = 1;

	vi di = { 1, 0, -1, 0 }, dj = { 0, 1, 0, -1 };

	while (!q.empty()) {

		int ui, uj; tie(ui, uj) = q.front(); q.pop();

		rep(k, 4) {

			int vi = ui + di[k], vj = uj + dj[k];			

			if (vi < 0 or vi >= H or vj < 0 or vj >= W) continue;

			if (!g[vi][vj] or d[vi][vj] != -1) continue;

			d[vi][vj] = d[ui][uj] + 1;

			q.push(make_pair(vi, vj));

		}

	}

	dbvv(d);	

	cout << (d[H - 1][W - 1] == -1 ? -1 : white - d[H - 1][W - 1]) << endl;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)

#define rep3(i, s, n) for (int i = s; i > (int)(n); i--)

#define all(obj) obj.begin(), obj.end()

#define db(x) cerr << #x << ":" << x << " "

#define dbl(x) cerr << #x << ":" << x << "\n"

#define dbv(vec) cerr << #vec << ":"; for (auto e : vec) cerr << e << ", "; cout << "\n"

#define dbvv(vv) cerr << #vv << ":\n"; for (auto vec : vv) { for (auto e : vec) cerr << e << ", "; cerr << "\n"; }

#define dbm(map) cerr << #map << ":"; for (auto p : map) cerr << "(" << p.first << ", " << p.second << "), "; cerr << "\n";

#define YN(f) cout << (f ? "YES" : "NO") << "\n"

#define Yn(f) cout << (f ? "Yes" : "No") << "\n"

#define yn(f) cout << (f ? "yes" : "no") << "\n"

using ll = long long;

using vi = vector<int>; using vl = vector<ll>; using vs = vector<string>; using vb = vector<bool>;

using vvi = vector<vi>; using vvl = vector<vl>; using vvb = vector<vb>;

using pii = pair<int, int>; using pll = pair<ll, ll>;

using vpii = vector<pii>; using vpll = vector<pll>;

using ti3 = tuple<int, int, int>; using tl3 = tuple<ll, ll, ll>;

using mii = map<int, int>; using mil = map<int, ll>; using mli = map<ll, int>; using mll = map<ll, ll>;



int main() {

	int H, W; cin >> H >> W;

	vvb g(H, vb(W));

	vvi d(H, vi(W, -1));

	int white = 0;

	rep(i, H) {

		string s; cin >> s;		

		rep(j, W)	{

			if (s[j] == '.') {

				g[i][j] = true;

				white++;

			}

		}

	}

	// dbvv(g); dbl(white);

	queue<pii> q;

	pii s(0, 0); q.push(s); d[0][0] = 1;

	vi di = { 1, 0, -1, 0 }, dj = { 0, 1, 0, -1 };

	while (!q.empty()) {

		int ui, uj; tie(ui, uj) = q.front(); q.pop();

		rep(k, 4) {

			int vi = ui + di[k], vj = uj + dj[k];			

			if (vi < 0 or vi >= H or vj < 0 or vj >= W) continue;

			if (!g[vi][vj] or d[vi][vj] != -1) continue;

			d[vi][vj] = d[ui][uj] + 1;

			q.push(make_pair(vi, vj));

		}

	}

	// dbvv(d);	

	cout << (d[H - 1][W - 1] != -1 ? white - d[H - 1][W - 1] : -1) << endl;

}

// Fastest known solution for this problem: