#include <bits/stdc++.h>
using namespace std;
char s[1 << 10][1 << 10];
bool vx[1 << 10], vy[1 << 10];
int dis[1 << 10][1 << 10];
queue<pair<int, int> > q;
pair<int, int> p1, p2;
int main() {
  int i, n, m, x, y, ans;
  while (scanf("%d%d", &n, &m) != EOF) {
    ans = 1 << 30;
    for (i = 0; i < n; i++) scanf("%s", s[i]);
    memset(dis, 0, sizeof(dis));
    memset(vx, 0, sizeof(vx));
    memset(vy, 0, sizeof(vy));
    while (!q.empty()) q.pop();
    vx[n - 1] = 1;
    for (i = 0; i < m; i++) {
      dis[n - 1][i] = 1;
      if (s[n - 1][i] == '#') {
        q.push(make_pair(n - 1, i));
      }
    }
    while (!q.empty()) {
      p1 = q.front();
      q.pop();
      x = p1.first;
      y = p1.second;
      if (x == 0) ans = min(ans, dis[x][y]);
      if (!vx[x]) {
        vx[x] = 1;
        for (i = 0; i < m; i++) {
          if (!dis[x][i]) {
            dis[x][i] = dis[x][y] + 1;
            if (s[x][i] == '#') q.push(make_pair(x, i));
          }
        }
      }
      if (!vy[y]) {
        vy[y] = 1;
        for (i = 0; i < n; i++) {
          if (!dis[i][y]) {
            dis[i][y] = dis[x][y] + 1;
            if (s[i][y] == '#') {
              q.push(make_pair(i, y));
            }
          }
        }
      }
    }
    if (ans > 1234560) ans = -1;
    printf("%d\n", ans);
  }
  return 0;
}
