#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

struct poz{
	int y, x;
	poz(int a, int b) {
		y = a, x = b;
	}
};

int w, h, i, j, m, n, p1, p2, p3, potezi, d,  y, x;
bool mat[505][505], vidjeno[505][505], insert;
char ch[505][505];

inline bool legit(int y, int x) {
	return x > 0 && y > 0 && x <= w && y <= h && mat[y][x];
}

inline bool kraj(int y, int x) {
	return x == w && y == h;
}


int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> h >> w;
	for (i = 1; i <= h; i++) {
		cin >> ch[i] + 1;
	}
	for (i = 1; i <= h; i++) {
		for (j = 1; j <= w; j++) {
			mat[i][j] = ch[i][j] == '.';
		}
	}

	vector<poz> v;
	v.push_back({ 1, 1 });
	i = d = 0;
	potezi = 0;
	insert = true;
	while (insert) {
		potezi++;
		d = v.size();
		insert = false;
		for (i; i < d; i++) {
			for (m = -1; m <= 1; m++) {
				for (n = -1; n <= 1; n++) {
					y = v[i].y + m;
					x = v[i].x + n;
					if (legit(y, x) && !vidjeno[y][x]) {
						v.push_back({ y, x });
						vidjeno[y][x] = insert = true;
						if (kraj(y, x)) {
							goto KRAJ;
						}
					}
					
				}
			}
		}
	}
	KRAJ:
	cout << (insert ? potezi : -1);
}