#include <bits/stdc++.h>

using namespace std;
int p[503][503];
bool vis[503][503];
int n, m;
struct Mesto {
    int x, y;
};
struct qMesto {
    Mesto pos;
    int daljina = 0;
};

int bfs(int poc1, int poc2)
{
    int daljina = 0;
    queue<qMesto> q;
    for (int i = 0; i <= n+1; i++) {
        vis[i][0] = true;
        vis[i][m+1] = true;
    }
    for (int i = 0; i <= m+1; i++) {
        vis[0][i] = true;
        vis[n+1][i] = true;
    }
    q.push({{poc1, poc2}, 0});
    qMesto mst;
    Mesto mss;
    while (!q.empty()) {
        mst = q.front();
        mss = mst.pos;
        q.pop();
        vis[mss.x][mss.y] = true;
        if (p[mss.x][mss.y] == -1)
            continue;
        if (mss.x == 1 && mss.y == 1)
            return mst.daljina;
        if (!vis[mss.x+1][mss.y+1])
            q.push({{mss.x+1, mss.y+1}, mst.daljina+1});
        if (!vis[mss.x][mss.y+1])
            q.push({{mss.x, mss.y+1}, mst.daljina+1});
        if (!vis[mss.x+1][mss.y])
            q.push({{mss.x+1, mss.y}, mst.daljina+1});
        if (!vis[mss.x-1][mss.y-1])
            q.push({{mss.x-1, mss.y-1}, mst.daljina+1});
        if (!vis[mss.x][mss.y-1])
            q.push({{mss.x, mss.y-1}, mst.daljina+1});
        if (!vis[mss.x-1][mss.y])
            q.push({{mss.x-1, mss.y}, mst.daljina+1});
        if (!vis[mss.x+1][mss.y-1])
            q.push({{mss.x+1, mss.y-1}, mst.daljina+1});
        if (!vis[mss.x-1][mss.y+1])
            q.push({{mss.x-1, mss.y+1}, mst.daljina+1});

    }
    return 0;
}

int main()
{
    string str;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> str;
        for (int j = 0; j < str.length(); j++)
        {
            if (str[j] == '.')
                p[i+1][j+1] = 0;
            else p[i+1][j+1] = -1;
        }
    }
    cout << bfs(n, m);
    return 0;
}