#include <bits/stdc++.h>
using namespace std;
long long labs(long long a) { return a < 0 ? (-a) : a; }
long long max(long long a, long long b) { return a > b ? a : b; }
long long min(long long a, long long b) { return a < b ? a : b; }
const int MAXN = 1005;
const int UNDEF = -1;
const int IMPOS = INT_MAX - 2;
char f[MAXN][MAXN];
int dph[MAXN], dpv[MAXN];
int n, m;
int RecV(int i);
int RecH(int j) {
  if (dph[j] != UNDEF) return dph[j];
  dph[j] = IMPOS;
  for (int i = 0; i < n; i++) {
    if (f[i][j] == '#') {
      dph[j] = min(dph[j], RecV(i) + 1);
    }
  }
  return dph[j];
}
int RecV(int i) {
  if (dpv[i] != UNDEF) return dpv[i];
  dpv[i] = IMPOS;
  for (int j = 0; j < m; j++) {
    if (f[i][j] == '#') {
      dpv[i] = min(dpv[i], RecH(j) + 1);
    }
  }
  return dpv[i];
}
int main() {
  cin >> n >> m;
  fill(dph, dph + MAXN, UNDEF);
  fill(dpv, dpv + MAXN, UNDEF);
  for (int(i) = 0; (i) < (n); (i)++) {
    cin >> f[i];
  }
  dpv[n - 1] = 0;
  int res = RecV(0);
  if (res == IMPOS) {
    res = -1;
  }
  cout << res << endl;
  return 0;
}
