#include <bits/stdc++.h>
using namespace std;
char str[1010][1010];
vector<int> edge[2012];
int flag[2012];
int step[2012];
int main() {
  int n, m, i, j;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) scanf("%s", str[i] + 1);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (str[i][j] == '#') {
        edge[i].push_back(j + n);
        edge[j + n].push_back(i);
      }
    }
  }
  memset(step, 0, sizeof(step));
  step[1] = 0;
  queue<int> Q;
  memset(flag, 0, sizeof(flag));
  Q.push(1);
  flag[1] = 1;
  int ans = -1;
  while (!Q.empty()) {
    int fr = Q.front();
    Q.pop();
    for (i = 0; i < edge[fr].size(); i++) {
      int to = edge[fr][i];
      if (to == n) {
        ans = step[fr] + 1;
        printf("%d\n", ans);
        return 0;
      }
      if (flag[to]) continue;
      step[to] = step[fr] + 1;
      Q.push(to);
      flag[to] = 1;
    }
  }
  printf("-1\n");
}
