#include <bits/stdc++.h>
using namespace std;
struct node1 {
  vector<int> a;
};
node1 pnt[2 * 1005];
int dist[2 * 1005];
int main() {
  int n, m;
  int i, j;
  int f;
  char c;
  int cnt = 0;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> c;
      if (c == '#') {
        pnt[i].a.push_back(j + n);
        pnt[j + n].a.push_back(i);
        cnt++;
      }
    }
  }
  queue<int> q;
  q.push(1);
  for (i = 1; i <= 2 * 1005; i++) {
    dist[i] = 1000000;
  }
  dist[1] = 0;
  while (!q.empty()) {
    f = q.front();
    q.pop();
    for (i = 0; i < pnt[f].a.size(); i++) {
      if (dist[pnt[f].a[i]] > dist[f] + 1) {
        q.push(pnt[f].a[i]);
        dist[pnt[f].a[i]] = dist[f] + 1;
      }
    }
  }
  int ans;
  ans = dist[n];
  if (ans == 1000000) ans = -1;
  cout << ans;
  return 0;
}
