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

vector<vector<int> > poteziKralj={{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
vector<vector<int> > poteziKonja={{-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {-2, -1}, {-2, 1}, {2, -1}, {2, 1}};
vector<vector<int> > poteziLovac={{-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
vector<vector<int> > poteziTop={{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
vector<vector<int> > poteziKraljica={{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
vector<int> promena{1,2,3,4,5};

struct figure
{
    int kralj;//0
    int lovac;//1
    int top;//2
    int konj;//3
    int kraljica;//4
};

int minimum(const int &g,const int &figura,const int &pravaFigura)
{
    if(g==INT_MAX)
        return INT_MAX;
    if(g>=0 && figura==pravaFigura)
        return g+1;
    if(g>=0 && figura!=pravaFigura)
        return g+promena[pravaFigura]+1;
    return INT_MIN;
}


int f(const vector<vector<bool> > &v,vector<vector<figure> > &mem,vector<vector<bool> > &slobodan,const int &h,const int &w,int x,int y,int figura)
{
    if(figura==0 && x==0 && y==0)
        return 0;
    if(figura==1 && x==0 && y==0)
        return 2;
    if(figura==2 && x==0 && y==0)
        return 3;
    if(figura==3 && x==0 && y==0)
        return 4;
    if(figura==4 && x==0 && y==0)
        return 5;
    if(figura==0 && mem[x][y].kralj>=0)
        return mem[x][y].kralj;
    if(figura==1 && mem[x][y].lovac>=0)
        return mem[x][y].lovac;
    if(figura==2 && mem[x][y].top>=0)
        return mem[x][y].top;
    if(figura==3 && mem[x][y].konj>=0)
        return mem[x][y].konj;
    if(figura==4 && mem[x][y].kraljica>=0)
        return mem[x][y].kraljica;

    if(x==0 && y==0)
        return 0;
    int mn=INT_MAX,g0,g1,g2,g3,g4,x1,y1,pravaFigura=0,mn1;
    slobodan[x][y]=false;

    if(figura==0)
    {
        pravaFigura=0;
        for (int i=0;i<poteziKralj.size();i++)
        {
            x1=x+poteziKralj[i][0];
            y1=y+poteziKralj[i][1];
            if(x1>=0 && x1<h && y1>=0 && y1<w && v[x1][y1]==true && slobodan[x1][y1]==true){
                g0=f(v,mem,slobodan,h,w,x1,y1,0);
                g1=f(v,mem,slobodan,h,w,x1,y1,1);
                g2=f(v,mem,slobodan,h,w,x1,y1,2);
                g3=f(v,mem,slobodan,h,w,x1,y1,3);
                g4=f(v,mem,slobodan,h,w,x1,y1,4);
                mn1=minimum(g0,0,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g1,1,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g2,2,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g3,3,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g4,4,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
            }
        }
        slobodan[x][y]=true;
        return mem[x][y].kralj=mn;
    }
    else if(figura==3)
    {
        pravaFigura=3;
        for (int i=0;i<poteziKonja.size();i++)
        {
            x1=x+poteziKonja[i][0];
            y1=y+poteziKonja[i][1];
            if(x1>=0 && x1<h && y1>=0 && y1<w && v[x1][y1]==true && slobodan[x1][y1]==true){
                g0=f(v,mem,slobodan,h,w,x1,y1,0);
                g1=f(v,mem,slobodan,h,w,x1,y1,1);
                g2=f(v,mem,slobodan,h,w,x1,y1,2);
                g3=f(v,mem,slobodan,h,w,x1,y1,3);
                g4=f(v,mem,slobodan,h,w,x1,y1,4);
                mn1=minimum(g0,0,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g1,1,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g2,2,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g3,3,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g4,4,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
            }
        }
        slobodan[x][y]=true;
        return mem[x][y].konj=mn;
    }
    else if(figura==1)
    {
        pravaFigura=1;
        for (int i=0;i<poteziLovac.size();i++)
        {
            x1=x+poteziLovac[i][0];
            y1=y+poteziLovac[i][1];
            while(x1>=0 && x1<h && y1>=0 && y1<w && v[x1][y1]==true && slobodan[x1][y1]==true)
            {
                g0=f(v,mem,slobodan,h,w,x1,y1,0);
                g1=f(v,mem,slobodan,h,w,x1,y1,1);
                g2=f(v,mem,slobodan,h,w,x1,y1,2);
                g3=f(v,mem,slobodan,h,w,x1,y1,3);
                g4=f(v,mem,slobodan,h,w,x1,y1,4);
                mn1=minimum(g0,0,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g1,1,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g2,2,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g3,3,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g4,4,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                x1+=poteziLovac[i][0];
                y1+=poteziLovac[i][1];
            }
        }
        slobodan[x][y]=true;
        return mem[x][y].lovac=mn;
    }
    else if(figura==2)
    {
        pravaFigura=2;
        for (int i=0;i<poteziTop.size();i++)
        {
            x1=x+poteziTop[i][0];
            y1=y+poteziTop[i][1];
            while(x1>=0 && x1<h && y1>=0 && y1<w && v[x1][y1]==true && slobodan[x1][y1]==true)
            {
                g0=f(v,mem,slobodan,h,w,x1,y1,0);
                g1=f(v,mem,slobodan,h,w,x1,y1,1);
                g2=f(v,mem,slobodan,h,w,x1,y1,2);
                g3=f(v,mem,slobodan,h,w,x1,y1,3);
                g4=f(v,mem,slobodan,h,w,x1,y1,4);
                mn1=minimum(g0,0,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g1,1,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g2,2,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g3,3,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g4,4,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                x1+=poteziTop[i][0];
                y1+=poteziTop[i][1];
            }
        }
        slobodan[x][y]=true;
        return mem[x][y].top=mn;
    }
    else if(figura==4)
    {
        pravaFigura=4;
        for (int i=0;i<poteziKraljica.size();i++)
        {
            x1=x+poteziKraljica[i][0];
            y1=y+poteziKraljica[i][1];
            while(x1>=0 && x1<h && y1>=0 && y1<w && v[x1][y1]==true && slobodan[x1][y1]==true)
            {
                g0=f(v,mem,slobodan,h,w,x1,y1,0);
                g1=f(v,mem,slobodan,h,w,x1,y1,1);
                g2=f(v,mem,slobodan,h,w,x1,y1,2);
                g3=f(v,mem,slobodan,h,w,x1,y1,3);
                g4=f(v,mem,slobodan,h,w,x1,y1,4);
                mn1=minimum(g0,0,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g1,1,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g2,2,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g3,3,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                mn1=minimum(g4,4,pravaFigura);
                if(mn1!=INT_MIN && mn1!=INT_MAX)
                    mn=min(mn,mn1);
                x1+=poteziKraljica[i][0];
                y1+=poteziKraljica[i][1];
            }
        }
        slobodan[x][y]=true;
        return mem[x][y].kraljica=mn;
    }
    return INT_MIN;
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int h,w,x;
    char c;
    cin >> h >> w;
    vector<vector<bool> > v(h,vector<bool> (w,false));
    vector<vector<bool> > slobodan(h,vector<bool> (w,true));
    vector<vector<figure> > mem(h,vector<figure> (w));
    for (int i=0;i<h;i++)
        for (int j=0;j<w;j++)
        {
            cin >> c;
            if(c=='.')
                v[i][j]=true;
            else
            {
                v[i][j]=false;
                slobodan[i][j]=false;
            }
            mem[i][j].kralj=INT_MIN;
            mem[i][j].lovac=INT_MIN;
            mem[i][j].top=INT_MIN;
            mem[i][j].konj=INT_MIN;
            mem[i][j].kraljica=INT_MIN;
        }
    mem[0][0].kralj=0;
    mem[0][0].lovac=0;
    mem[0][0].top=0;
    mem[0][0].konj=0;
    mem[0][0].kraljica=0;
    int resenje=INT_MAX;
    for (int i=0;i<5;i++)
    {
            x=f(v,mem,slobodan,h,w,h-1,w-1,i);
            if(x>=0 && x<resenje)
                resenje=x;
    }
    if(resenje==INT_MAX)
        cout << -1;
    else
        cout << resenje;
}