#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const long long LINF = 1e17 + 7;
const double Pi = acos(-1.);
const double EPS = 1e-8;
int n, m;
char a[N][N];
int d[N][N], inq[N][N];
char rChar() {
  char ch = getchar();
  while (!(ch == '.' || ch == '#')) ch = getchar();
  return ch;
}
queue<int> que;
int solve() {
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      d[i][j] = INF;
      inq[i][j] = false;
    }
  for (int j = 0; j < m; ++j)
    if (a[n - 1][j] == '#') {
      d[n - 1][j] = 1;
      inq[n - 1][j] = true;
      que.push(((n - 1) << 10 | j) << 1);
    }
  int ret = INF;
  while (!que.empty()) {
    int x, y, p, tmp = que.front();
    x = tmp >> 11;
    y = (tmp ^ (x << 11)) >> 1;
    p = tmp & 1;
    que.pop();
    if (x == 0) ret = min(ret, d[x][y]);
    if (d[x][y] + 1 < ret) {
      if (p != 1)
        for (int i = 0; i < n; ++i)
          if (a[i][y] == '#' && d[i][y] > d[x][y] + 1) {
            d[i][y] = d[x][y] + 1;
            if (!inq[i][y]) {
              inq[i][y] = true;
              que.push((i << 10 | y) << 1 | 1);
            }
          }
      if (p != 0)
        for (int j = 0; j < m; ++j) {
          if (a[x][j] == '#' && d[x][j] > d[x][y] + 1) {
            d[x][j] = d[x][y] + 1;
            if (!inq[x][j]) {
              inq[x][j] = true;
              que.push((x << 10 | j) << 1 | 0);
            }
          }
        }
    }
    inq[x][y] = false;
  }
  return ret != INF ? ret : -1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      a[i][j] = rChar();
    }
  printf("%d\n", solve());
  return 0;
}
