#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
#define INF 0x3f3f3f3f3f3f3f3f
#define MOD 1000000007
using namespace std;
vector<ll>rows[1005];
vector<ll>columns[1005];
bool vis[1005][1005];
struct point{
ll x;
ll y;
ll step;
bool way;
};
void solve(){
deque<point>de;
ll n,m; bool flag=0;
cin>>n>>m;
for(ll i=1;i<=n;i++){
for(ll j=1;j<=m;j++){
char t;
cin>>t;
if(t=='#') {rows[i].push_back(j);columns[j].push_back(i);if(i==1&&j==1)flag=1;}
}
}
ll ans=INF;
vis[1][1]=1;
if(flag) de.push_back({1,1,1,0});
de.push_front({1,1,0,1});
while(!de.empty()){
point tem=de.front();
de.pop_front();
vis[tem.x][tem.y]=1;
if(tem.x==n){
if(tem.way) ans=min(ans,tem.step);
else ans=min(ans,tem.step+1);
}
if(tem.way){
for(ll i=0;i<rows[tem.x].size();i++){
if(vis[tem.x][rows[tem.x][i]]) continue;
if(rows[tem.x][i]==tem.y) continue;
vis[tem.x][rows[tem.x][i]]=1;
de.push_front({tem.x,rows[tem.x][i],tem.step,tem.way});
de.push_back({tem.x,rows[tem.x][i],tem.step+1,!tem.way});
}
}else{
for(ll i=0;i<columns[tem.y].size();i++){
if(vis[columns[tem.y][i]][tem.y]) continue;
if(columns[tem.y][i]==tem.x) continue;
vis[columns[tem.y][i]][tem.y]=1;
de.push_front({columns[tem.y][i],tem.y,tem.step,tem.way});
de.push_back({columns[tem.y][i],tem.y,tem.step+1,!tem.way});
}
}
}
if(ans==INF) cout<<-1<<endl;
else
std::cout<<ans<<endl;
}
int main(){
ios::sync_with_stdio(0);
std::cin.tie(0);std::cout.tie(0);
ll t=1;
while(t--) solve();
return 0;
}