#include<iostream>
#include<queue>
using namespace std;

int main()
{

    int h, w;
    cin >> h >> w;
    char T[h][w];

    for(int i=0; i<h; i++)
        for(int j=0; j<w; j++)
            cin >> T[i][j];

    int D[h][w];
    for(int i=0; i<h; i++)
        for(int j=0; j<w; j++)
            D[i][j] = -1;

    D[0][0] = 0;

    queue< pair<int,int> > Q;
    Q.push({0,0});
    int found = 0;

    while(Q.size())
    {
        if(Q.front().first==h && Q.front().second==w)
        {
            cout << D[h][w];
            found = 1;
            break;
        }
        for(int i=Q.front().first-1; i<=Q.front().first+1; i++)
            for(int j=Q.front().second-1; j<=Q.front().second+1; j++)
                if(0<=i && i<h && 0<=j && j<w)
                    if(T[i][j]=='.' && D[i][j]==-1)
                    {
                        D[i][j] = D[Q.front().first][Q.front().second] + 1;
                        Q.push({i, j});
                    }

    }

    return 0;
}