#include <bits/stdc++.h>
using namespace std;
long long n, m, d[2005];
vector<long long> g[2005];
void solve() {
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      char c;
      cin >> c;
      if (c == '#') g[i].push_back(n + j), g[n + j].push_back(i);
    }
  }
  fill(d, d + n + m, 1e10);
  queue<long long> q;
  d[n - 1] = 0;
  q.push(n - 1);
  while (!q.empty()) {
    long long u = q.front();
    q.pop();
    for (long long i : g[u]) {
      if (d[i] == 1e10) {
        d[i] = d[u] + 1;
        q.push(i);
      }
    }
  }
  cout << (d[0] == 1e10 ? -1 : d[0]) << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
