#include <bits/stdc++.h>
using namespace std;
const int M = 2000005;
int n, m, ans = 1e9, d[M], v[M];
int head[M], ver[2 * M], Next[2 * M], edge[2 * M], tot;
char s[1005][1005];
void add(int x, int y) {
  Next[++tot] = head[x], head[x] = tot;
  ver[tot] = y;
}
void bfs() {
  for (int i = 2; i <= n + m; i++) d[i] = 1e9;
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  q.push(make_pair(0, 1));
  while (!q.empty()) {
    int x = q.top().second;
    q.pop();
    v[x] = 1;
    for (int i = head[x]; i; i = Next[i]) {
      int y = ver[i];
      if (d[y] > d[x] + 1) {
        d[y] = d[x] + 1;
        if (!v[y]) q.push(make_pair(d[y], y));
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j <= m; j++)
      if (s[i][j] == '#') add(i, n + j), add(n + j, i);
  }
  bfs();
  if (d[n] == 1e9)
    puts("-1");
  else
    printf("%d\n", d[n]);
  return 0;
}
