#include <bits/stdc++.h>
using namespace std;
const int maxN = 2048;
int N, M;
char str[maxN][maxN];
const int inf = 100000000;
int dist[maxN];
vector<int> v[maxN];
queue<int> q;
bool vst[maxN];
int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N; ++i) scanf("%s", str[i]);
  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }
  if (M == 1) {
    if (str[0][0] == '#' && str[N - 1][0] == '#')
      cout << 2 << endl;
    else
      cout << -1 << endl;
    return 0;
  }
  bool ok = 0;
  for (int j = 0; j < M; ++j)
    if (str[0][j] == '#') {
      ok = 1;
      break;
    }
  if (!ok) {
    cout << -1 << endl;
    return 0;
  }
  ok = 0;
  for (int j = 0; j < M; ++j)
    if (str[N - 1][j] == '#') {
      ok = 1;
      break;
    }
  if (!ok) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < N + M; ++i) dist[i] = inf, v[i].clear();
  for (int i = 0, j; i < N; ++i)
    for (j = 0; j < M; ++j)
      if (str[i][j] == '#') {
        v[i].push_back(j + N);
        v[j + N].push_back(i);
      }
  dist[0] = 0;
  q.push(0);
  memset(vst, 0, sizeof(vst));
  vst[0] = 1;
  int u, ans = -1;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    vst[u] = 0;
    for (int i = 0; i < (int)v[u].size(); ++i) {
      if (u == N - 1 && dist[u] < inf) {
        if (ans == -1 || ans > dist[u]) ans = dist[u];
      }
      if (dist[u] + 1 < dist[v[u][i]]) {
        dist[v[u][i]] = dist[u] + 1;
        if (!vst[v[u][i]]) {
          vst[v[u][i]] = 1;
          q.push(v[u][i]);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
