#include <bits/stdc++.h>
using namespace std;
struct times
{
    int x,y,t;
    times(int x,int y,int t)
    {
        this -> x = x;
        this -> y = y;
        this -> t = t;
    }
}temp;
int dir[4][2] = {0,1,1,0,0,-1,-1,0};
int h,w,c,flag = 0;
char mp[51][51];
int vis[51][51];
int main()
{
    cin>>h>>w;
    for(int i = 0;i < h;i ++)
    {
        cin.get();
        for(int j = 0;j < w;j ++)
        {
            cin>>mp[i][j];
            if(mp[i][j] == '.')c ++;
        }
    }
    if(mp[0][0] == '.' && mp[h - 1][w - 1] == '.')
    {
        queue<times> q;
        q.push(times(0,0,1));
        vis[0][0] = 1;
        while(!q.empty())
        {
            if(flag)break;
            temp = q.front();
            q.pop();
            for(int i = 0;i < 4;i ++)
            {
                int tx = temp.x + dir[i][0];
                int ty = temp.y + dir[i][1];
                if(tx < 0 || ty < 0 || tx >= h || ty >= w || vis[tx][ty] || mp[tx][ty] == '#')continue;
                vis[tx][ty] = 1;
                if(tx == h - 1 && ty == w - 1)flag = temp.t + 1;
                q.push(times(tx,ty,temp.t + 1));
            }
        }
    }
    if(flag)cout<<c - flag;
    else cout<<-1;
}