#include<iostream>
using namespace std;
struct wz{
int x,y,c;
};
string a[45];
int vis[45][45];
int k[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n,m;
int s(wz w,int o,int u){
if (w.xn-1 && w.ym-1){
return w.c;
}
for (int i = 0;i<4;i++){
int x=k[i][0]+w.x;
int y=k[i][1]+w.y;
if(x<o && x>=0 &&
y<u && y>=0 &&
vis[y][x]==0){
// cout <<x<<' '<<y<<endl;
if (a[y][x]!='#'){
// cout << w.x << ' ' << w.y << endl;
vis[y][x]=1;
int l=s({x,y,w.c+1},o,u);
vis[y][x]=0;
if(l!=-1){
return l;
}
}
}
}
return -1;
}
int main(){
cin >> n >> m;
for (int i = 0;i<m;i++){
cin >> a[i];
}
wz k={0,0,1};
cout << s(k,n,m);
}