#include <bits/stdc++.h>
using namespace std;
template <class T>
void in(T &x) {
  x = 0;
  bool f = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = 1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  if (f) x = -x;
}
int v[2005 * 2005], nx[2005 * 2005];
int head[2005], cnte;
inline void adde(int uu, int vv) {
  v[++cnte] = vv, nx[cnte] = head[uu];
  head[uu] = cnte;
}
char tc[2005];
int tot;
int q[2005], hd, tl;
int d[2005];
bool vis[2005];
int n, m;
signed main() {
  in(n), in(m);
  tot = n + m;
  for (register int i = 1; i <= n; ++i) {
    scanf("%s", tc + 1);
    for (register int j = 1; j <= m; ++j)
      if (tc[j] == '#') {
        adde(i, n + j);
        adde(n + j, i);
      }
  }
  memset(d, -1, sizeof d);
  q[hd = tl = 1] = n;
  d[n] = 0;
  while (hd <= tl) {
    int x = q[hd++];
    for (register int i = head[x]; i; i = nx[i])
      if (!vis[v[i]]) {
        vis[v[i]] = 1;
        d[v[i]] = d[x] + 1;
        q[++tl] = v[i];
      }
  }
  printf("%d", d[1]);
  return 0;
}
