#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
//ここから編集する


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


    [] {
        [] {
            [] {
                int H, W;
                cin >> H >> W;
                vector<vector<int> > vv(H, vector<int>(W));
                int nokori = H * W;
                rep(i, 0, H) {
                    rep(j, 0, W) {
                        char tmp;
                        cin >> tmp;
                        if (tmp == '.') {
                            vv[i][j] = 2147483647 / 3;
                        } else {
                            vv[i][j] = -1;
                            nokori--;
                        }
                    }
                }
                vv[0][0] = 0;
                auto comv = [&](int h, int w) {
                    return h * W + w;
                };
                auto rcomh = [&](int n) {
                    return n / W;
                };
                auto rcomw = [&](int n) {
                    return n % W;
                };
                auto check = [&](int h, int w) {
                    return h >= 0 && h < H && w >= 0 && w < W;
                };
                queue<int> que;
                que.push(comv(0, 0));
                int dx[] = {1, 0, -1, 0};
                int dy[] = {0, 1, 0, -1};
                int ans = 0;
                while (!que.empty()) {
                    int h = rcomh(que.front());
                    int w = rcomw(que.front());
                    que.pop();

                    rep(i, 0, 4) {
                        if (check(h + dy[i], w + dx[i])) {
                            if (vv[h + dy[i]][w + dx[i]] >= 0 && vv[h + dy[i]][w + dx[i]] > vv[h][w] + 1) {
                                vv[h + dy[i]][w + dx[i]] = vv[h][w] + 1;
                                que.push(comv(h + dy[i], w + dx[i]));
                            }
                        }
                    }

                }
                if (vv[H - 1][W - 1] >= 2147483647 / 3) {
                    cout << -1 << endl;
                    return;
                }
                cout << nokori - vv[H - 1][W - 1] - 1 << endl;


            }();

        }();
    }();

    return 0;
}


Measured memory_bytes: 888