#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define mp make_pair
#define ALL(x) x.begin(),x.end()
#define F first
#define S second
using namespace std;
typedef long long ll;
const int maxn=500000+5;
const int INF=0x3f3f3f3f;
const int P=1000000007;
const double PI=acos(-1.0);
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
template <class T1, class T2>inline void gmax(T1 &a,T2 b){if (b>a) a=b;}
template <class T1, class T2>inline void gmin(T1 &a,T2 b){if (b<a) a=b;}
void up(int&x,int y){x+=y;if(x>=P)x-=P;}
/*int n,a;
int main(){
    read(n),read(a);
    if (a>=n) puts("Yes");
    else{
        for (int i=0;i<=a;i++){
            if ((n-i)%500==0) return puts("Yes"),0;
        }
        puts("No");
    }
    return 0;
}*/
/*int n,a[105];
int main(){
    read(n);
    for (int i=1;i<=n;i++) read(a[i]);
    sort(a+1,a+1+n);
    reverse(a+1,a+1+n);
    int ans=0;
    for (int i=1;i<=n;i+=2){
        ans+=a[i]-a[i+1];
    }
    printf("%d\n",ans);
    return 0;
}*/
/*int c[4][4];
int main(){
    int ans=0;
    for (int i=1;i<=3;i++){
        for (int j=1;j<=3;j++){
            ans+=read(c[i][j]);
        }
    }
    if (ans%3!=0) puts("No");
    else{
        bool flag=true;
        int tmp;

        tmp=c[1][2]-c[1][1];
        for (int i=2;i<=3;i++){
            flag&=(tmp==c[i][2]-c[i][1]);
        }

        tmp=c[1][3]-c[1][2];
        for (int i=2;i<=3;i++){
            flag&=(tmp==c[i][3]-c[i][2]);
        }

        tmp=c[2][1]-c[1][1];
        for (int i=2;i<=3;i++){
            flag&=(tmp==c[2][i]-c[1][i]);
        }
        tmp=c[3][1]-c[2][1];
        for (int i=2;i<=3;i++){
            flag&=(tmp==c[3][i]-c[2][i]);
        }

        if (flag) puts("Yes");
        else puts("No");
    }
}*/
struct Node{
    int x,y,step;
};
int n,m;
char maze[100][100];
bool vis[100][100];
int dir_x[]={0,1,0,-1};
int dir_y[]={1,0,-1,0};
int main(){
    read(n),read(m);
    int ans=n*m;
    for (int i=1;i<=n;i++){
        scanf("%s",maze[i]+1);
        for (int j=1;j<=m;j++){
            if (maze[i][j]=='#') ans--;
        }
    }
    if (maze[1][1]=='#'||maze[n][m]=='#') puts("-1");
    else{
        queue<Node>Q;
        Q.push((Node){1,1,0});

        while (!Q.empty()){
            Node cur=Q.front();Q.pop();
            if (cur.x==n&&cur.y==m){
                return printf("%d\n",ans-cur.step-1),0;
            }
            for (int i=0;i<4;i++){
                int tx=dir_x[i]+cur.x;
                int ty=dir_y[i]+cur.y;
                if (tx<1||tx>n||ty<1||ty>m||maze[tx][ty]=='#'||vis[tx][ty]) continue;
                vis[tx][ty]=1;
                Q.push((Node){tx,ty,cur.step+1});
            }
        }
        puts("-1");
    }
    return 0;
}


Measured memory_bytes: 256