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

struct init_ { init_(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } init_;

template<class C> using iodef_rng_t = typename enable_if<decltype(declval<C>().begin(), true_type{})::value && !is_same<C, string>::value>::type;
template<class C, class = iodef_rng_t<C>> istream& operator>>(istream& in, C& c) { for (auto& v : c) { in >> v; } return in; }
template<class C, class = iodef_rng_t<C>> ostream& operator<<(ostream& out, const C& c) { int i = 0; for (const auto& v : c) { if (i++) { out << ' '; } out << v; } return out; }
template<size_t N, class T, class R> using when_last = typename enable_if<N == tuple_size<remove_reference_t<T>>::value - 1, R>::type;
template<size_t N, class T, class R> using when_init = typename enable_if<N != tuple_size<remove_reference_t<T>>::value - 1, R>::type;
template<size_t N = 0, class T> auto operator>>(istream& in, T&& t) -> when_last<N, T, istream&> { return in >> get<N>(t); }
template<size_t N = 0, class T> auto operator>>(istream& in, T&& t) -> when_init<N, T, istream&> { return operator>><N + 1>(in >> get<N>(t), forward<T>(t)); }
template<size_t N = 0, class T> auto operator<<(ostream& out, const T& t) -> when_last<N, T, ostream&> { return out << get<N>(t); }
template<size_t N = 0, class T> auto operator<<(ostream& out, const T& t) -> when_init<N, T, ostream&> { return operator<<<N + 1>(out << get<N>(t) << ' ', t); }

#ifdef LOCAL
template<class T> void trace_(const char* s, const T& v) { cerr << s << "=" << v; }
template<class T, class... Ts> void trace_(const char* s, const T& v, const Ts&... vs) { for (; *s != ','; ++s) { cerr << *s; } cerr << "=" << v << ","; trace_(++s, vs...); }
#define TRACE(...) (cerr << "\033[2m\033[33m", trace_(#__VA_ARGS__, __VA_ARGS__), cerr << "\033[0m\n")
#else
#define TRACE(...)
#endif

template<class T> struct Grid {
    vector<T> data;
    const size_t h, w;
    Grid(size_t h, size_t w, T v = T()) : data(h * w, v), h(h), w(w) {}
    T& operator()(size_t i, size_t j) { return data[i * w + j]; }
    auto adjacent_indexes(size_t i, size_t j) const {
        vector<pair<size_t, size_t>> vs;
        if (i > 0) vs.emplace_back(i - 1, j);
        if (j > 0) vs.emplace_back(i, j - 1);
        if (i + 1 < h) vs.emplace_back(i + 1, j);
        if (j + 1 < w) vs.emplace_back(i, j + 1);
        return vs;
    }
    friend istream& operator>>(istream& in, Grid& g) {
        for (auto& v : g.data) in >> v;
        return in;
    }
    friend ostream& operator<<(ostream& out, const Grid& g) {
        for (size_t i = 0; i < g.h; ++i) {
            cout << g.data[i * g.w];
            for (size_t j = 1; j < g.w; ++j) {
                out << " " << g.data[i * g.w + j];
            }
            out << "\n";
        }
        return out;
    }
};

int main() {
    int h, w;
    cin >> h >> w;
    Grid<char> g(h, w);
    cin >> g;
    Grid<int> d(h, w, -1);
    queue<pair<int, int>> qu;
    qu.emplace(0, 0);
    d(0, 0) = 0;
    while (!qu.empty()) {
        auto p = qu.front(); qu.pop();
        for (auto q : g.adjacent_indexes(p.first, p.second)) {
            if (g(q.first, q.second) == '.' && d(q.first, q.second) == -1) {
                d(q.first, q.second) = d(p.first, p.second) + 1;
                qu.push(q);
            }
        }
    }
    TRACE(d);
    int sp = d(h-1, w-1);
    int ws = 0;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (g(i, j) == '.') ws++;
        }
    }
    TRACE(sp, ws);
    if (sp == -1) {
        cout << -1 << '\n';
    } else {
        cout << ws - sp - 1 << '\n';
    }
}


Measured memory_bytes: 256