#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair <int,int> p;
const int N = 505;
const int inf = 1000000000;

struct cvor{
    short i,j,v,w;
};

typedef pair <int,cvor> pi;

bool a[N][N];
vector <cvor> g[N][N][5];
int n,m;
int dist[N][N][5];
bool gotov[N][N][5];
bool got[N][N][4];

class cmp{
public:
    bool operator ()(pi a,pi b){
        return a.first > b.first;
    }
};

int brgrana = 0;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    //freopen("test.txt","r",stdin);
    cin >> n >> m;
    string s;
    for(int i = 1; i <= n; i++){
        cin >> s;
        for(int j = 1; j <= m; j++){
            a[i][j] = (s[j-1] == '#');
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(a[i][j]){
                continue;
            }
            ///zavrsi topa,lovca i kraljicu
            ///lovac
            if(!got[i][j][1]){
                int x = i,y = j;
                int poslx = 0,posly = 0;
                while(x >= 1 && x <= n && y >= 1 && y <= m && !a[x][y]){
                    poslx = x,posly = y;
                    got[x][y][1] = 1;
                    x--,y++;
                }
                x = i,y = j;
                int poslx1 = 0,posly1 = 0;
                while(x >= 1 && x <= n && y >= 1 && y <= m && !a[x][y]){
                    poslx1 = x,posly1 = y;
                    got[x][y][1] = 1;
                    x++,y--;
                }
                x = poslx1,y = posly1;
                while(x >= 1 && x <= n && y >= 1 && y <= m && !a[x][y]){
                    if(!(x == poslx && y == posly)){
                        g[x][y][1].push_back({poslx,posly,1,1});
                        g[poslx][posly][1].push_back({x,y,1,1});
                        g[x][y][4].push_back({poslx,posly,4,1});
                        g[poslx][posly][4].push_back({x,y,4,1});
                        brgrana += 4;
                    }
                    if(!(x == poslx1 && y == posly1)){
                        g[x][y][1].push_back({poslx1,posly1,1,1});
                        g[poslx1][posly1][1].push_back({x,y,1,1});
                        g[x][y][4].push_back({poslx1,posly1,4,1});
                        g[poslx1][posly1][4].push_back({x,y,4,1});
                        brgrana += 4;
                    }
                    x--,y++;
                }
            }
            if(!got[i][j][3]){
                int x = i,y = j;
                int poslx = 0,posly = 0;
                while(x >= 1 && x <= n && y >= 1 && y <= m && !a[x][y]){
                    poslx = x,posly = y;
                    got[x][y][3] = 1;
                    x--,y--;
                }
                x = i,y = j;
                int poslx1 = 0,posly1 = 0;
                while(x >= 1 && x <= n && y >= 1 && y <= m && !a[x][y]){
                    poslx1 = x,posly1 = y;
                    got[x][y][3] = 1;
                    x++,y++;
                }
                x = poslx1,y = posly1;
                while(x >= 1 && x <= n && y >= 1 && y <= m && !a[x][y]){
                    if(!(x == poslx && y == posly)){
                        g[x][y][1].push_back({poslx,posly,1,1});
                        g[poslx][posly][1].push_back({x,y,1,1});
                        g[x][y][4].push_back({poslx,posly,4,1});
                        g[poslx][posly][4].push_back({x,y,4,1});brgrana += 4;
                    }
                    if(!(x == poslx1 && y == posly1)){
                        g[x][y][1].push_back({poslx1,posly1,1,1});
                        g[poslx1][posly1][1].push_back({x,y,1,1});
                        g[x][y][4].push_back({poslx1,posly1,4,1});
                        g[poslx1][posly1][4].push_back({x,y,1,1});brgrana += 4;
                    }
                    x--,y--;
                }
            }
            ///top
            if(!got[i][j][0]){
                int x = i,y = j;
                int poslx = 0,posly = 0;
                while(x >= 1 && x <= n && y >= 1 && y <= m && !a[x][y]){
                    poslx = x,posly = y;
                    got[x][y][0] = 1;
                    y++;
                }
                x = i,y = j;
                int poslx1 = 0,posly1 = 0;
                while(x >= 1 && x <= n && y >= 1 && y <= m && !a[x][y]){
                    poslx1 = x,posly1 = y;
                    got[x][y][0] = 1;
                    y--;
                }
                x = poslx1,y = posly1;
                while(x >= 1 && x <= n && y >= 1 && y <= m && !a[x][y]){
                    if(!(x == poslx && y == posly)){
                        g[x][y][2].push_back({poslx,posly,2,1});
                        g[poslx][posly][2].push_back({x,y,2,1});
                        g[x][y][4].push_back({poslx,posly,4,1});
                        g[poslx][posly][4].push_back({x,y,4,1});brgrana += 4;
                    }
                    if(!(x == poslx1 && y == posly1)){
                        g[x][y][2].push_back({poslx1,posly1,2,1});
                        g[poslx1][posly1][2].push_back({x,y,2,1});
                        g[x][y][4].push_back({poslx1,posly1,4,1});
                        g[poslx1][posly1][4].push_back({x,y,4,1});brgrana += 4;
                    }
                    y++;
                }
            }
            if(!got[i][j][2]){
                int x = i,y = j;
                int poslx = 0,posly = 0;
                while(x >= 1 && x <= n && y >= 1 && y <= m && !a[x][y]){
                    poslx = x,posly = y;
                    got[x][y][2] = 1;
                    x--;
                }
                x = i,y = j;
                int poslx1 = 0,posly1 = 0;
                while(x >= 1 && x <= n && y >= 1 && y <= m && !a[x][y]){
                    poslx1 = x,posly1 = y;
                    got[x][y][2] = 1;
                    x++;
                }
                x = poslx1-1,y = posly1;
                while(x >= 1 && x <= n && y >= 1 && y <= m && !a[x][y]){
                    if(!(x == poslx && y == posly)){
                        g[x][y][2].push_back({poslx,posly,2,1});
                        g[poslx][posly][2].push_back({x,y,2,1});
                        g[x][y][4].push_back({poslx,posly,4,1});
                        g[poslx][posly][4].push_back({x,y,4,1});brgrana += 4;
                    }
                    if(!(x == poslx1 && y == posly1)){
                        g[x][y][2].push_back({poslx1,posly1,2,1});
                        g[poslx1][posly1][2].push_back({x,y,2,1});
                        g[x][y][4].push_back({poslx1,posly1,4,1});
                        g[poslx1][posly1][4].push_back({x,y,4,1});brgrana += 4;
                    }
                    x--;
                }
            }
            //cout << i << " " << j << " " << brgrana << endl;
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(a[i][j]){
                continue;
            }
            ///isti
            for(int k = 0; k < 5; k++){
                for(int l = 0; l < 5; l++){
                    if(k == l){
                        continue;
                    }
                    g[i][j][k].push_back({i,j,l,l+1});
                    brgrana++;
                }
            }
            ///kralj
            for(int k = -1; k <= 1; k++){
                for(int l = -1; l <= 1; l++){
                    if(l == 0 && k == 0){
                        continue;
                    }
                    if(i+k >= 1 && i+k <= n && j+l >= 1 && j+l <= m && !a[i+k][j+l]){
                        g[i][j][0].push_back({i+k,j+l,0,1});
                        brgrana++;
                    }
                }
            }
            ///konj
            for(int k = 1; k <= 2; k++){
                for(int z1 = -1; z1 <= 1; z1++){
                    if(z1 == 0){
                        continue;
                    }
                    for(int z2 = -1; z2 <= 1; z2++){
                        if(z2 == 0){
                            continue;
                        }
                        int nx = i+k*z1,ny = j+(3-k)*z2;
                        if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && !a[nx][ny]){
                            g[i][j][3].push_back({i+k*z1,j+(3-k)*z2,3,1});
                            brgrana++;
                        }
                    }
                }
            }
            //cout << i << " " << j << " " << brgrana << endl;
            ///kraljica gotova, da li mi uopste treba?
        }
    }
    /*for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            for(int v = 0; v < 5; v++){
                cout << "cvor " << i << " " << j << " " << v << endl;
                for(auto f : g[i][j][v]){
                    cout << f.i << " " << f.j << " " << f.v << " " << f.w << endl;
                }
            }
        }
    }*/
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            for(int v = 0; v < 5; v++){
                dist[i][j][v] = 1000000000;
            }
        }
    }
    dist[1][1][0] = 0;
    priority_queue <pi,vector<pi>,cmp> pq;
    cvor poc = {1,1,0,0};
    pq.push(make_pair(0,poc));
    while(!pq.empty()){
        cvor u = pq.top().second;
        //cout << "na cvoru " << u.i << " " << u.j << " " << u.v << endl;
        pq.pop();
        gotov[u.i][u.j][u.v] = 1;
        for(auto f : g[u.i][u.j][u.v]){
            int vi = f.i,vj = f.j,t = f.v,cost = f.w;
            //cout << "komsija " << vi << " " << vj << " " << t << " " << cost << endl;
            if(!gotov[vi][vj][t] && dist[vi][vj][t] > dist[u.i][u.j][u.v] + cost){
                dist[vi][vj][t] = dist[u.i][u.j][u.v]+cost;
                pq.push(pi(dist[vi][vj][t],f));
            }
        }
    }
    int sol = min({dist[n][m][0],dist[n][m][1],dist[n][m][2],dist[n][m][3],dist[n][m][4]});
    if(sol == inf){
        cout << -1 << endl;
    }
    else{
        cout << sol << endl;
    }
}