#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<string> a;
vector<vector<int>> dp;
vector<bool> uc, ur;
int32_t main() {
  ios_base ::sync_with_stdio(false);
  cin >> n >> m;
  a.resize(n);
  dp.resize(n, vector<int>(m, (int)1e7));
  ur.resize(n, false);
  uc.resize(m, false);
  ur[0] = true;
  queue<pair<int, int>> q;
  for (auto &i : a) {
    cin >> i;
  }
  for (int i = 0; i < m; ++i) {
    if (a[0][i] == '#') {
      dp[0][i] = 1;
      q.push({0, i});
    }
  }
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    if (!uc[y]) {
      for (int i = 0; i < n; ++i) {
        if (dp[i][y] > dp[x][y] + 1 && a[i][y] == '#') {
          dp[i][y] = dp[x][y] + 1;
          q.push({i, y});
        }
      }
      uc[y] = true;
    }
    if (!ur[x]) {
      ur[x] = true;
      for (int i = 0; i < m; ++i) {
        if (dp[x][i] > dp[x][y] + 1 && a[x][i] == '#') {
          dp[x][i] = dp[x][y] + 1;
          q.push({x, i});
        }
      }
    }
  }
  int ans = 1e7;
  for (int i = 0; i < m; ++i) {
    if (dp[n - 1][i] < ans) {
      ans = dp[n - 1][i];
    }
  }
  if (ans == 1e7) {
    cout << -1;
  } else {
    cout << ans;
  }
}
