//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

//int→string
//string→int
//------------------------------------------
inline int toInt(string s) {
    int v;
    istringstream sin(s);
    sin >> v;
    return v;
}

template<class T>
inline string toString(T x) {
    ostringstream sout;
    sout << x;
    return sout.str();
}

//sqrt
//-------------------------------------------
template<class T>
inline T sqr(T x) { return x * x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<VI> VVI;
typedef vector<VB> VVB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())

//repetition
//------------------------------------------
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define REP(i, n)  FOR(i,0,n)
#define REPR(i, n) for(int i = n;i >= 0;i--)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;


struct Point {
    Point() :
            x(0), y(0) {

    }

    Point(int _x, int _y) :
            x(_x), y(_y) {

    }

    int x;
    int y;

    Point operator+(Point &p) {
        return Point(x + p.x, y + p.y);
    }

};


int bfs(VS &mapS, VVI &mapI, VVB &mapB, int H, int W) {

    Point start = Point(0, 0);
    Point goal = Point(W-1, H-1);

    deque<Point> Q;
    Q.push_front(start);

    bool arrive = false;

    Point points[] = {
            Point(-1,0),
            Point(1,0),
            Point(0,-1),
            Point(0,1)
    };

    mapI[start.y][start.x] = 0;
    mapB[start.y][start.x] = true;

    while(!Q.empty()){
        Point p = Q.back(); Q.pop_back();

        REP(i,4){

            Point point = p + points[i];

            if(p.x == goal.x&&p.y ==goal.y){
                arrive = true;
                break;
            }

            if(point.x<0||point.x>=W||point.y<0||point.y>=H){
                continue;
            }

            if(mapB[point.y][point.x]){
                continue;
            }

            if(mapS[point.y][point.x]=='#'){
                continue;
            }

            mapB[point.y][point.x] = true;
            mapI[point.y][point.x] = mapI[p.y][p.x] + 1;
            Q.push_front(point);

        }

    }

    if(!arrive){
        mapI[goal.y][goal.x] = -1;
    }

    return mapI[goal.y][goal.x];

}

int main() {

    int H;
    int W;
    int whiteCount = 0;
    int count = 0;
    cin >> H >> W;

    VS mapS(H);
    REP(i, H) {
        cin >> mapS[i];
    }

    VVI mapI;
    mapI.resize(H);
    VVB mapB;
    mapB.resize(H);
    REP(i, H) {
        mapI[i].resize(W);
        mapB[i].resize(W);
    }
    REP(i, H) {
        REP(j, W) {
            mapI[i][j] = 0;
            mapB[i][j] = false;
            if (mapS[i][j] == '.') {
                whiteCount++;
            }
        }
    }

    count = bfs(mapS, mapI, mapB, H, W);

    if(count==-1){
        cout<<-1;
    }else{
        cout<<whiteCount-count-1;
    }

    return 0;
}






































































Measured memory_bytes: 256