// Slower C++ solution:
#include <iostream> // cout, endl, cin

#include <string> // string, to_string, stoi

#include <vector> // vector

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound

#include <utility> // pair, make_pair

#include <tuple> // tuple, make_tuple

#include <cstdint> // int64_t, int*_t

#include <cstdio> // printf

#include <map> // map

#include <queue> // queue, priority_queue

#include <set> // set

#include <stack> // stack

#include <deque> // deque

#include <unordered_map> // unordered_map

#include <unordered_set> // unordered_set

#include <bitset> // bitset

#include <cctype> // isupper, islower, isdigit, toupper, tolower

using namespace std;

#define int long long





signed main(){

    cin.tie(nullptr);

    ios::sync_with_stdio(false);



    int h,w; cin >> h >> w;

    vector<vector<char> > s(h+2,vector<char>(w+2));

    for(int i = 1;i < h+1;i ++){

        for(int j = 1;j < w+1;j ++){

            cin >> s.at(i).at(j);

        }

    }



    vector<vector<int> > t(h+2,vector<int>(w+2,10000));

    t.at(0).at(1) = 0;



    for(int i = 0;i < 100;i ++){

        for(int j = 1;j < h+1;j ++){

            for(int k = 1;k < w+1;k ++){

                if(s.at(j).at(k) == '.'){

                    t.at(j).at(k) = min(t.at(j).at(k),t.at(j).at(k-1)+1);

                    t.at(j).at(k) = min(t.at(j).at(k),t.at(j).at(k+1)+1);

                    t.at(j).at(k) = min(t.at(j).at(k),t.at(j-1).at(k)+1);

                    t.at(j).at(k) = min(t.at(j).at(k),t.at(j+1).at(k)+1);

                }

            }

        }



    }



    int count = 0;

    for(int i = 1;i < h+1;i ++){

        for(int j = 1;j < w+1;j ++){

            if(s.at(i).at(j) == '.') count ++;

        }

    }



    if(t.at(h).at(w) == 10000){

        cout << -1 << endl;

    }else{

        cout << count - t.at(h).at(w) << endl;

    }



    

}

// Faster C++ solution for the same problem (measured speedup: 1.2529895071030563x):
#include <iostream> // cout, endl, cin

#include <string> // string, to_string, stoi

#include <vector> // vector

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound

#include <utility> // pair, make_pair

#include <tuple> // tuple, make_tuple

#include <cstdint> // int64_t, int*_t

#include <cstdio> // printf

#include <map> // map

#include <queue> // queue, priority_queue

#include <set> // set

#include <stack> // stack

#include <deque> // deque

#include <unordered_map> // unordered_map

#include <unordered_set> // unordered_set

#include <bitset> // bitset

#include <cctype> // isupper, islower, isdigit, toupper, tolower

#include <math.h>

using namespace std;

#define int long long





signed main(){

    cin.tie(nullptr);

    ios::sync_with_stdio(false);



    int h,w; cin >> h >> w;

    vector<vector<char> > s(h,vector<char>(w));

    for(int i = 0;i < h;i ++){

        for(int j = 0;j < w;j ++){

            cin >> s.at(i).at(j);

        }

    }

    int b = 0;

    for(int i = 0;i < h;i ++){

        for(int j = 0;j < w;j ++){

            if(s.at(i).at(j) == '.') b ++;

        }

    }



    b -= 2;



    vector<vector<int> > itta(h,vector<int>(w)),count(h,vector<int>(w));

    for(int i = 0;i < h;i ++){

        for(int j = 0;j < w;j ++){

            if(s.at(i).at(j) == '#'){

                itta.at(i).at(j) = 1;

            }

        }

    }



    itta.at(0).at(0) = 1;



    queue<int> qx,qy;

    qx.push(0);

    qy.push(0);

    vector<int> dx(4),dy(4);

    dx.at(0)=-1;

    dx.at(1)=0;

    dx.at(2)=1;

    dx.at(3)=0;

    dy.at(0)=0;

    dy.at(1)=1;

    dy.at(2)=0;

    dy.at(3)=-1;



    while(qx.size()>0){

        int x = qx.front();

        int y = qy.front();

        for(int i = 0;i < 4;i ++){

            if(0 <= x+dx.at(i) && x+dx.at(i)<h && 0 <= y+dy.at(i) && y+dy.at(i) < w){

                if(itta.at(x+dx.at(i)).at(y+dy.at(i)) == 0){

                    itta.at(x+dx.at(i)).at(y+dy.at(i)) = 1;

                    count.at(x+dx.at(i)).at(y+dy.at(i)) = count.at(x).at(y) +1;

                    qx.push(x+dx.at(i));

                    qy.push(y+dy.at(i));

                }

            }

        }



        qx.pop();

        qy.pop();

    }



    if(count.at(h-1).at(w-1) == 0) cout << -1 << endl;

    else cout << b - count.at(h-1).at(w-1) + 1 << endl; 



}