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

int n, m, dist[maxn][maxn], d[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
string a[maxn];

bool ok(int i, int j){
	return !(i < 0 or j < 0 or i >= n or j >= m or a[i][j] == '#');
}

void bfs(){
	queue<pair<int, int> > q;
	q.push({0, 0});
	while(!q.empty()){
		int x = q.front().first, y = q.front().second;
		q.pop();
		for(int i = 0; i < 8; ++i){
			int i1 = x + d[i][0], j1 = y + d[i][1];
			if(ok(i1, j1) and dist[i1][j1] > dist[x][y] + 1){
				dist[i1][j1] = dist[x][y] + 1;
				q.push({i1, j1});
			}
		}
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	cin >> n >> m;
	for(int i = 0; i < n; ++i){
		cin >> a[i];
	}
	for(int i = 0; i < n; ++i){
		for(int i1= 0; i1 < m; ++i1){
			dist[i][i1] = 1e9;
		}
	}
	dist[0][0] = 0;
	bfs();
	if(dist[n-1][m-1] == 1e9){
		cout << -1;
		return 0;
	}
	cout << dist[n-1][m-1] << '\n';
}