#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <utility>
#include <tuple>
#include <cctype>
#include <bitset>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pint;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> tint;
typedef vector<int> vint;
typedef vector<ll> vll;
int dx[8]={0,0,-1,1,1,1,-1,-1};
int dy[8]={-1,1,0,0,1,-1,1,-1};
const int SIZE=500;
//ここまでテンプレ
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(auto i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) :
    src(src), dst(dst), weight(weight) { }
};
bool operator < (const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
    e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;
void shortestPath(const Graph &g, int s,
    vector<Weight> &dist, vector<int> &prev) {
  int n = g.size();
  dist.assign(n, INF); dist[s] = 0;
  prev.assign(n, -1);
  priority_queue<Edge> Q; // "e < f" <=> "e.weight > f.weight"
  for (Q.push(Edge(-2, s, 0)); !Q.empty(); ) {
    Edge e = Q.top(); Q.pop();
    if (prev[e.dst] != -1) continue;
    prev[e.dst] = e.src;
    FOR(f,g[e.dst]) {
      if (dist[f->dst] > e.weight+f->weight) {
        dist[f->dst] = e.weight+f->weight;
        Q.push(Edge(f->src, f->dst, e.weight+f->weight));
      }
    }
  }
}
vector<int> buildPath(const vector<int> &prev, int t) {
  vector<int> path;
  for (int u = t; u >= 0; u = prev[u])
    path.push_back(u);
  reverse(path.begin(), path.end());
  return path;
}
//↓二次元→一次元座標圧縮：vector<pair<int,int>>を入れると対応関係をmap[旧座標(pair<int,int>)]=新座標(int)で返す
map<pair<int,int>,int> comp(vector<pair<int,int>> v){
	map<pair<int,int>,int> pam;
	int sz=v.size();
	for(int i=0;i<sz;i++){
		pam[v[i]]=0;
	}
	//新座標が1からほしいときはnc=1にする
	int nc=0;
	for(pair<pair<int,int>,int> i:pam){
		pam[i.first]=nc;
		nc++;
	}
	return pam;
}
//↑座標圧縮
int main(){
	int H,W;
	cin>>H>>W;
	int white=0;
	char C[SIZE][SIZE]={};
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			cin>>C[i][j];
			if(C[i][j]=='.')
				white++;
		}
	}
	vector<pint> vec;
	for(int i=0;i<H;i++)
		for(int j=0;j<W;j++)
			vec.pb(mp(i,j));
	map<pint,int> pam=comp(vec); 
	Graph G;
	for(int i=0;i<pam.size();i++){
		Edges E;
		G.pb(E);
	}
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			for(int k=0;k<4;k++){
				int x=i+dx[k],y=j+dy[k];
				if(x<0 || H<=x || y<0 || W<=y)
					continue;
				if(C[x][y]=='.')
					G[pam[mp(i,j)]].pb(Edge(pam[mp(i,j)],pam[mp(x,y)],1));
			}
		}
	}

	vint D,P;
	shortestPath(G,pam[mp(0,0)],D,P);
	if(D[pam[mp(H-1,W-1)]]==INF)
		cout<<-1<<endl;
	else
		cout<<white-D[pam[mp(H-1,W-1)]]-1<<endl;
	return 0;
}

Measured memory_bytes: 896