#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1 << 21;
const ll INF = 1LL << 60;
const ll mod = 1e9+7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

// 088d
int h, w;
string s[1000];
int ans = 0;
int d[100][100];

void bfs(){
    queue<P> q;
    q.push(P(0, 0));
    d[0][0] = 0;
    while(!q.empty()){
        P tmpQ = q.front();
        q.pop();
        rep(i, 4){
            if (tmpQ.first==0){
                if(tmpQ.second==0){
                    if(i==1 or i==0) continue;
                    if (s[tmpQ.first+dy[i]][tmpQ.second+dx[i]]=='#' or d[tmpQ.first+dy[i]][tmpQ.second+dx[i]]!=1<<21) continue;
                    d[tmpQ.first+dy[i]][tmpQ.second+dx[i]] = d[tmpQ.first][tmpQ.second]+1;
                    q.push(P(tmpQ.first+dy[i], tmpQ.second+dx[i]));
                }else if (tmpQ.second==w-1){
                    if(i==1 or i==2) continue;
                    if (s[tmpQ.first+dy[i]][tmpQ.second+dx[i]]=='#' or d[tmpQ.first+dy[i]][tmpQ.second+dx[i]]!=1<<21) continue;
                    d[tmpQ.first+dy[i]][tmpQ.second+dx[i]] = d[tmpQ.first][tmpQ.second]+1;
                    q.push(P(tmpQ.first+dy[i], tmpQ.second+dx[i]));
                }else{
                    if(i==1) continue;
                    if (s[tmpQ.first+dy[i]][tmpQ.second+dx[i]]=='#' or d[tmpQ.first+dy[i]][tmpQ.second+dx[i]]!=1<<21) continue;
                    d[tmpQ.first+dy[i]][tmpQ.second+dx[i]] = d[tmpQ.first][tmpQ.second]+1;
                    q.push(P(tmpQ.first+dy[i], tmpQ.second+dx[i]));
                }
            }else if (tmpQ.first==h-1){
                if (tmpQ.second==0){
                    if (i==3 or i==0) continue;
                    if (s[tmpQ.first+dy[i]][tmpQ.second+dx[i]]=='#' or d[tmpQ.first+dy[i]][tmpQ.second+dx[i]]!=1<<21) continue;
                    d[tmpQ.first+dy[i]][tmpQ.second+dx[i]] = d[tmpQ.first][tmpQ.second]+1;
                    q.push(P(tmpQ.first+dy[i], tmpQ.second+dx[i]));
                }else if (tmpQ.second==w-1){
                    if (i==3 or i==2) continue;
                    if (s[tmpQ.first+dy[i]][tmpQ.second+dx[i]]=='#' or d[tmpQ.first+dy[i]][tmpQ.second+dx[i]]!=1<<21) continue;
                    d[tmpQ.first+dy[i]][tmpQ.second+dx[i]] = d[tmpQ.first][tmpQ.second]+1;
                    q.push(P(tmpQ.first+dy[i], tmpQ.second+dx[i]));
                }else{
                    if(i==3) continue;
                    if (s[tmpQ.first+dy[i]][tmpQ.second+dx[i]]=='#' or d[tmpQ.first+dy[i]][tmpQ.second+dx[i]]!=1<<21) continue;
                    d[tmpQ.first+dy[i]][tmpQ.second+dx[i]] = d[tmpQ.first][tmpQ.second]+1;
                    q.push(P(tmpQ.first+dy[i], tmpQ.second+dx[i]));
                }
            }else{
                if (tmpQ.second==0){
                    if (i==0) continue;
                    if (s[tmpQ.first+dy[i]][tmpQ.second+dx[i]]=='#' or d[tmpQ.first+dy[i]][tmpQ.second+dx[i]]!=1<<21) continue;
                    d[tmpQ.first+dy[i]][tmpQ.second+dx[i]] = d[tmpQ.first][tmpQ.second]+1;
                    q.push(P(tmpQ.first+dy[i], tmpQ.second+dx[i]));
                }else if (tmpQ.second==w-1){
                    if (i==2) continue;
                    if (s[tmpQ.first+dy[i]][tmpQ.second+dx[i]]=='#' or d[tmpQ.first+dy[i]][tmpQ.second+dx[i]]!=1<<21) continue;
                    d[tmpQ.first+dy[i]][tmpQ.second+dx[i]] = d[tmpQ.first][tmpQ.second]+1;
                    q.push(P(tmpQ.first+dy[i], tmpQ.second+dx[i]));
                }else{
                    if (s[tmpQ.first+dy[i]][tmpQ.second+dx[i]]=='#' or d[tmpQ.first+dy[i]][tmpQ.second+dx[i]]!=1<<21) continue;
                    d[tmpQ.first+dy[i]][tmpQ.second+dx[i]] = d[tmpQ.first][tmpQ.second]+1;
                    q.push(P(tmpQ.first+dy[i], tmpQ.second+dx[i]));
                }
            }
        }
    }
}

int main(){
    cin >> h >> w;
    rep(i, h) cin >> s[i];
    rep(i, h)rep(j, w) d[i][j] = 1<<21;
    rep(i, h)rep(j, w) {
        if (s[i][j]=='#') ans++;
    }
    bfs();
    if (d[h-1][w-1]==1<<21) cout << -1 << endl;
    else cout << h*w - d[h-1][w-1] -1 - ans<< endl;
    return 0;
}


Measured memory_bytes: 256