#include <bits/stdc++.h>
const int N = 1010;
char s[N][N];
bool row[N], col[N];
int map[N][N];
struct unit {
  int x, y, d;
};
std::queue<unit> Q;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", s[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) map[i][j] = 1 << 30;
  for (int i = 0; i < m; i++)
    if (s[0][i] == '#') {
      Q.push((unit){0, i, 0});
      map[0][i] = 0;
    }
  row[0] = true;
  int ans = 1 << 30;
  while (!Q.empty()) {
    unit A = Q.front();
    Q.pop();
    if (A.x == n - 1) ans = ans < (A.d + 1) ? ans : (A.d + 1);
    if (!row[A.x]) {
      row[A.x] = true;
      for (int i = 0; i < m; i++)
        if (s[A.x][i] == '#' && map[A.x][i] > A.d + 1) {
          map[A.x][i] = A.d + 1;
          Q.push((unit){A.x, i, A.d + 1});
        }
    }
    if (!col[A.y]) {
      col[A.y] = true;
      for (int i = 0; i < n; i++)
        if (s[i][A.y] == '#' && map[i][A.y] > A.d + 1) {
          map[i][A.y] = A.d + 1;
          Q.push((unit){i, A.y, A.d + 1});
        }
    }
  }
  if (ans >= (1 << 30))
    puts("-1");
  else
    printf("%d\n", ans);
  scanf(" ");
}
