#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

//#define LOCAL 0
//#ifdef LOCAL
//#define DEBUG(s) cout << (s) << endl
//#define dump(x)  cerr << #x << " = " << (x) << endl
//#define BR cout << endl;
//#else
//#define DEBUG(s) do{}while(0)
//#define dump(x) do{}while(0)
//#define BR
//#endif


//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30)
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007
//ここから編集する

struct GridBFS {
    vector<vector<int> > mat;
    int H, W;

    GridBFS(int h, int w) {
        H = h;
        W = w;
        mat.resize(H, vector<int>(W, INF));
    }

    void setFloor(int h, int w) {
        mat[h][w] = 0;
    }

    void setWall(int h, int w) {
        mat[h][w] = -1;
    }

    void init(int h, int w) {
        H = h;
        W = w;
        mat.resize(H, vector<int>(W, INF));
    }

    int get(int h, int w) {
        return mat[h][w];
    }

    bool check(int h, int w) {
        return h < H && h >= 0 && w < W && w >= 0;
    }

    /**
     *
     * @param sh スタートのhの座標
     * @param sw スタートのwの座標
     */
    void calc(int sh, int sw) {
        typedef pair<int, int> P;
        queue<pair<int, int> > que;
        que.push(P(sh, sw));
        mat[sh][sw] = 0;
        int dh[] = {0, 1, 0, -1};
        int dw[] = {1, 0, -1, 0};
        while (!que.empty()) {
            int h = que.front().first;
            int w = que.front().second;
            que.pop();
            for (int i = 0; i < 4; i++) {
                int newH = h + dh[i];
                int newW = w + dw[i];
                if (check(newH, newW) && this->get(newH, newW) != 0 && this->get(newH, newW) > this->get(h, w) + 1) {
                    mat[newH][newW] = mat[h][w] + 1;
                    que.push(P(newH, newW));
                }
            }
        }
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int H, W;
    cin >> H >> W;
    GridBFS gridBFS(H, W);
    int white = 0;
    rep(i, 0, H) {
        rep(j, 0, W) {
            char tmp;
            cin >> tmp;
            if (tmp == '#') {
                gridBFS.setWall(i, j);
            } else {
                white++;
            }
        }
    }
    gridBFS.calc(0, 0);
    if (gridBFS.get(H - 1, W - 1) == INF) {
        cout << -1 << endl;
    } else {
        int ans = white - gridBFS.get(H - 1, W - 1) - 1;
        cout << ans << endl;
    }


    return 0;
}


Measured memory_bytes: 384