#include <bits/stdc++.h>
using namespace std;
int n, m;
bool a[1001][1001];
bool used[1001][1001];
struct T {
  int i, j, d;
  T(int i, int j, int d) : i(i), j(j), d(d) {}
};
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      char ch;
      cin >> ch;
      if (ch == '#') a[i][j] = true;
    }
  queue<T> q;
  q.push(T(n, 0, 0));
  used[n][0] = true;
  while (!q.empty()) {
    int i = q.front().i, j = q.front().j, d = q.front().d;
    q.pop();
    if (i == 1) {
      cout << d;
      return 0;
    }
    if (i != 0)
      for (int k = 1; k <= m; k++)
        if (a[i][k] && !used[0][k]) {
          q.push(T(0, k, d + 1));
          used[0][k] = true;
        }
    if (j != 0)
      for (int k = 1; k <= n; k++)
        if (a[k][j] && !used[k][0]) {
          q.push(T(k, 0, d + 1));
          used[k][0] = true;
        }
  }
  cout << -1;
  return 0;
}
