#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
struct edge {
  int to, next;
} e[2 * maxn * maxn];
int head[maxn * 2], np;
void adde(int u, int v) {
  e[++np] = (edge){v, head[u]};
  head[u] = np;
  e[++np] = (edge){u, head[v]};
  head[v] = np;
}
int n, m, d[maxn * 2];
void bfs() {
  queue<int> q;
  memset(d, 1, sizeof(d));
  q.push(1);
  d[1] = 0;
  while (!q.empty()) {
    int i = q.front();
    q.pop();
    for (int p = head[i]; p; p = e[p].next) {
      int j = e[p].to;
      if (d[i] + 1 < d[j]) d[j] = d[i] + 1, q.push(j);
    }
  }
}
int main() {
  char ch;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> ch;
      if (ch == '#') adde(i, j + n);
    }
  bfs();
  printf("%d\n", d[n] > maxn * maxn ? -1 : d[n]);
  return 0;
}
