#include <bits/stdc++.h>
using namespace std;
int h, w;

struct cord
{
    int x, y, dist;
};

int bfs(char **x)
{
    int mov_row[] = {0, 0, 1, -1, -1, 1, -1, 1};
    int mov_col[] = {1, -1, 0, 0, -1, 1, 1, -1};

    queue<struct cord> q;
    bool visited[h][w] = { };
    visited[0][0] = true;
    struct cord c1 = { 0, 0, 0 };
    q.push(c1);

    while(!q.empty())
    {
        struct cord s = q.front();
        q.pop();

        for(int i = 0; i < 8; i++)
        {
            int r = s.x + mov_row[i];
            int c = s.y + mov_col[i];
            if((r < h && r >= 0) && (c < w && c >= 0) && (r == h - 1) && (c == w - 1) && x[r][c] == '.') return s.dist + 1;
            if((r < h && r >= 0) && (c < w && c >= 0) && x[r][c] == '.' && !visited[r][c])
            {
                visited[r][c] = true;
                struct cord c2 = {r, c, s.dist + 1};
                q.push(c2);
            }
        }
    }
    return -1;
}

int main()
{
    scanf("%d%d", &h, &w); getchar();
    char **x = (char**) malloc(h * sizeof(char*));
    for(int i = 0; i < h; i++) x[i] = (char*) malloc(w * sizeof(char));

    for(int i = 0; i < h; i++)
    {
        for(int j = 0; j < w; j++)
        {
            scanf("%c", &x[i][j]);
        }
        getchar();
    }
    printf("%d", bfs(x));
}