#include <stdio.h>
#include <stdlib.h>
#include <utility>
#include <queue>
short int pomeraj[2][8]={{-1,-1,-1,0,0,1,1,1},{-1,0,1,-1,1,-1,0,1}};

bool izlaziIzMatrice(int n,int m,std::pair<int,int> tacka)
{
    if((tacka.first>=0&&tacka.first<n)&&(tacka.second>=0&&tacka.second<m)) return false;
    return true;
}
bool bfs(int n,int m,bool **matrica,std::pair<int,int> kraj)
{
    bool obidjeni[n][m]={};
    int rastojanje[n][m]={};
    std::queue<std::pair<int,int>> proces;
    proces.push({0,0});
    std::pair<int,int> temp,temp1;
    obidjeni[0][0]=true;
    while(!proces.empty())
    {
        temp=proces.front();
        proces.pop();
        for(int i=0;i<8;i++)
        {
            temp1={temp.first+pomeraj[0][i],temp.second+pomeraj[1][i]};
            if(!izlaziIzMatrice(n,m,temp1)&&!obidjeni[temp1.first][temp1.second]&&matrica[temp1.first][temp1.second])
            {
                proces.push(temp1);
                rastojanje[temp1.first][temp1.second]=rastojanje[temp.first][temp.second]+1;
                obidjeni[temp1.first][temp1.second]=true;
                if(temp1.first==kraj.first&&temp1.second==kraj.second)
                {
                    printf("%d",rastojanje[kraj.first][kraj.second]);
                    return true;
                }
            }

        }
    }
    return false;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    bool** tabla=(bool**)malloc(n*sizeof(bool*));

    getchar();
    char c;
    for(int  i=0;i<n;i++)
    {
        tabla[i]=(bool*)calloc(m,sizeof(bool));
        for(int j=0;j<m;j++)
        {
            c=getchar();
            if(c=='.') tabla[i][j]=true;
        }

        getchar();
    }
    if(!bfs(n,m,tabla,{n-1,m-1})) printf("-1");
    return 0;
}