// Main maut ko takiya, aur kafan ko chadaar banakar audhta hoon!

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

typedef long long ll;
typedef unsigned long long ull;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define len(a) ll(a.size())
#define endl "\n"

#define vll vector< ll >
#define pll pair< ll, ll >
#define vvll vector< vll >
#define vpll vector< pll >

#define umsll unordered_map<string,ll>
#define umll unordered_map<ll,ll>
#define sll set<ll>
#define minpqll priority_queue<ll,vector<ll>,greater<ll>>

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)

    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1)
    {
        cerr << name << " : " << arg1 << endl;
    }

    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args)
    {
        const char* comma = strchr(names + 1, ',');
        cerr.write(names, comma - names) << " : " << arg1 << "";
        __f(comma + 1, args...);
    }

#else
#define trace(...)
#endif // ifndef ONLINE_JUDGE

const double pi=3.141592653589793238462643383279502884197169399375105820974944;
const double e=2.71828182845904523536028747135266249775724709369995;
const ll mod=1e9+7;
const ll inf=2e18;
const ll ninf=-2e18;
const ll nax=1e6;

ll power(ll x, ll y, ll mod)
{
    ll ans=1;
    x=x%mod;
    while(y)
    {
        if(y&1)
            ans=(x*ans)%mod;

        x=(x*x)%mod;
        y>>=1;
    }
    return ans;
}

bool done[50][50];
ll dist[50][50];
string s[50];
int h,w;

void bfs()
{
    for(int i = 0; i < h; i++)
        for(int j = 0; j < w; j++)
        {
            done[i][j] = false;
            dist[i][j] = inf;
        }

    queue< pair<ll,ll> > q;

    done[0][0] = true;
    q.push({0,0});
    dist[0][0] = 0;
    ll i,j;
    while(!q.empty())
    {
        tie(i,j) = q.front();
        q.pop();

        if(i-1 >= 0 and !done[i-1][j] and s[i-1][j] != '#')
        {
            dist[i-1][j] = dist[i][j] + 1;
            done[i-1][j] = true;
            q.push({i-1,j});
        }

        if(j-1 >= 0 and !done[i][j-1] and s[i][j-1] != '#')
        {
            dist[i][j-1] = dist[i][j] + 1;
            done[i][j-1] = true;
            q.push({i,j-1});
        }
        if(i+1 < h and !done[i+1][j] and s[i+1][j] != '#')
        {
            dist[i+1][j] = dist[i][j] + 1;
            done[i+1][j] = true;
            q.push({i+1,j});
        }
        if(j+1 < w and !done[i][j+1] and s[i][j+1] != '#')
        {
            dist[i][j+1] = dist[i][j] + 1;
            done[i][j+1] = true;
            q.push({i,j+1});
        }
    }
}

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

    cin>>h>>w;


    for(int i = 0; i < h; i++)
        cin>>s[i];

    ll cnt = 0;

    for(int i = 0; i < h; i++)
    {
        for(int j = 0; j < w; j++)
        {
            if(s[i][j] == '.')
                cnt++;
        }
    }

    bfs();

    if(dist[h-1][w-1] != inf)
    {
        cout<<cnt - 2 -(dist[h-1][w-1] -1);
    }
    else
        cout<<-1;


    return 0;
}


Measured memory_bytes: 256