#include <bits/stdc++.h>
using namespace std;

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                  \
    cerr << #v ":[ ";            \
    for (auto macro_vi : v) {    \
        cerr << macro_vi << " "; \
    }                            \
    cerr << "]" << endl;
#define ddump(v)                           \
    cerr << #v ":" << endl;                \
    for (auto macro_row : v) {             \
        cerr << "[";                       \
        for (auto macro__vi : macro_row) { \
            cerr << macro__vi << " ";      \
        }                                  \
        cerr << "]" << endl;               \
    }
using lint       = long long;
const int INF    = 1e9;
const lint LINF  = 1e18;
const double EPS = 1e-10;

int main() {
    int H, W;
    cin >> H >> W;

    vector<string> G(H);
    rep(i, H) cin >> G[i];

    int cnt = 0;
    rep(i, H) rep(j, W) {
        if (G[i][j] == '#') { cnt++; }
    }

    vector<vector<lint>> dist(H, vector<lint>(W, LINF));
    dist[0][0] = 0;
    queue<pair<int, int>> q({{0, 0}});
    while (!q.empty()) {
        int cx, cy;
        tie(cx, cy) = q.front();
        q.pop();
        int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
        for (int i = 0; i < 4; i++) {
            int nx = cx + dx[i], ny = cy + dy[i];
            if (0 <= nx && nx < H && 0 <= ny && ny < W && G[nx][ny] == '.' &&
                dist[nx][ny] == LINF) {
                dist[nx][ny] = dist[cx][cy] + 1;
                q.push({nx, ny});
            }
        }
    }
    // ddump(G);
    // ddump(dist);

    if (dist[H - 1][W - 1] == LINF) {
        cout << -1 << endl;
    } else {

        cout << (H * W - cnt) - (dist[H - 1][W - 1] + 1) << endl;
    }

    return 0;
}
#include <vector>
#include <queue>
#include <iostream>
#include <set>
#include <algorithm>
#define ii pair<int, int>
#define iii pair<ii, int>
#define pq priority_queue

using namespace std;

int bfs(vector<vector<char>> graph, ii start, ii goal, int h, int w) {
    queue<iii> possible;
    set<ii> visited;
    possible.push(make_pair(start, 0));
    while (!possible.empty()) {
        ii xy = possible.front().first;
        int dist_from_start = possible.front().second;
        possible.pop();
        if (visited.find(xy) == visited.end()) {
            if (xy == goal) {
                return dist_from_start;
            }
            if (xy.first + 1 < h && graph[xy.first + 1][xy.second] == '.') {
                possible.push(make_pair(make_pair(xy.first + 1, xy.second), dist_from_start + 1));
            }
            if (xy.first - 1 >= 0 && graph[xy.first - 1][xy.second] == '.') {
                possible.push(make_pair(make_pair(xy.first - 1, xy.second), dist_from_start + 1));
            }
            if (xy.second - 1 >= 0 && graph[xy.first][xy.second - 1] == '.') {
                possible.push(make_pair(make_pair(xy.first, xy.second - 1), dist_from_start + 1));
            }
            if (xy.second + 1 < w && graph[xy.first][xy.second + 1] == '.') {
                possible.push(make_pair(make_pair(xy.first, xy.second + 1), dist_from_start + 1));
            }
            visited.insert(xy);
        }
    }
    return -1;
}

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

    int h, w;
    cin >> h >> w;
    vector<vector<char>> graph(h, vector<char>(w));
    cin.get();
    int no_black = 0;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin.get(graph[i][j]);
            if (graph[i][j] == '#') {
                no_black++;
            }
        }
        cin.get(); //get \n character
    }
    int answer = bfs(graph, make_pair(0, 0), make_pair(h - 1, w - 1), h, w);
    if (answer >= 0) {
        answer = h * w - answer - no_black - 1;
    }
    cout << answer;
}