#include <bits/stdc++.h>
using namespace std;
struct node {
  int dian, next;
};
node bin[3200000];
int g[3100], ge, dui[3100];
bool use[3100];
char ss[1100][1100];
void jia(int x, int y) {
  ge++;
  bin[ge].dian = y;
  bin[ge].next = g[x];
  g[x] = ge;
}
int main() {
  int n, m, s, t, t1, step, ke, d;
  bool check;
  scanf("%d %d", &n, &m);
  for (int j = 1; j <= n; j++) {
    scanf("%s", ss[j]);
  }
  for (int j = 1; j <= n + m; j++) g[j] = 0;
  ge = 0;
  for (int j = 1; j <= n; j++)
    for (int i = 1; i <= m; i++) {
      if (ss[j][i - 1] == '#') {
        jia(j, n + i);
        jia(n + i, j);
      }
    }
  for (int j = 1; j <= n + m; j++) use[j] = false;
  use[n] = true;
  check = false;
  s = t = t1 = 1;
  dui[1] = n;
  step = 0;
  while (s <= t) {
    step++;
    for (int j = s; j <= t; j++) {
      ke = g[dui[j]];
      while (ke != 0) {
        d = bin[ke].dian;
        if (!use[d]) {
          use[d] = true;
          t1++;
          dui[t1] = d;
          if (d == 1) {
            check = true;
            break;
          }
        }
        ke = bin[ke].next;
      }
      if (check) break;
    }
    if (check) break;
    s = t + 1;
    t = t1;
  }
  if (check)
    printf("%d\n", step);
  else
    printf("-1\n");
  return 0;
}
