#include <bits/stdc++.h>
using namespace std;
vector<int> a[1005], b[1005];
int visiteda[1005], visitedb[1005], f = 1;
queue<int> q1, q2;
void bfs(int n) {
  int size, s = 1, i, c = 0;
  q2.push(s);
  visiteda[s] = 1;
  while ((!q1.empty()) || (!q2.empty())) {
    c++;
    while (!q2.empty()) {
      s = q2.front();
      for (i = 0, size = a[s].size(); i < size; i++) {
        if (!visitedb[a[s][i]]) {
          visitedb[a[s][i]] = 1;
          q1.push(a[s][i]);
        }
      }
      q2.pop();
    }
    c++;
    while (!q1.empty()) {
      s = q1.front();
      for (i = 0, size = b[s].size(); i < size; i++) {
        if (!visiteda[b[s][i]]) {
          visiteda[b[s][i]] = 1;
          if (b[s][i] == n) {
            f = 0;
            break;
          }
          q2.push(b[s][i]);
        }
      }
      q1.pop();
    }
    if (f == 0) {
      cout << c << endl;
      return;
    }
  }
  cout << -1 << endl;
}
int main() {
  int s, n, m, i, u, v, j;
  char c;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> c;
      if (c == '#') {
        a[i].push_back(j);
        b[j].push_back(i);
      }
    }
  }
  bfs(n);
  return 0;
}
