#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int h, w;

    cin >> h >> w;

    int ar[h][w];
    int rw[8] = {-1, 0, 0, 1, -1, 1, -1, 1};
    int cl[8] = {0, -1, 1, 0, -1, 1, 1, -1};

    for(int row = 0; row < h; row++)
    {
        char c;
        for(int col = 0; col < w; col++)
        {
            cin >> c;
            c == '#' ? ar[row][col] = 0 : ar[row][col] = 1;
        }
    }

    bool pos[h][w];
    memset(pos, false, sizeof pos);

    pos[0][0] = true;

    queue<pair<pair<int,int>, int>> q;

    q.push(make_pair(make_pair(0, 0), 0));

    while(!q.empty())
    {
        pair<pair<int,int>, int> t = q.front();

        q.pop();

        int y = t.first.first;
        int x = t.first.second;

        if(y == h-1 && x == w-1)
        {
            cout << t.second;
            return 0;
        }

        for(int i = 0; i < 8; i++)
        {
            int row = y + rw[i];
            int col = x + cl[i];

            if(col >= 0 && row >= 0 && row < h && col < w && ar[row][col] && !pos[row][col])
            {
                pos[row][col] = true;
                q.push(make_pair(make_pair(row,col), t.second+1));
            }
        }

    }

    cout << "-1";
}