#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T maxer(T &a, T b) {
  a = max(a, b);
}
template <typename T>
inline T miner(T &a, T b) {
  a = min(a, b);
}
void iOS() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void fin() { freopen("in", "r", stdin); }
void fout() { freopen("out", "w", stdout); }
const int inf = ~0u / 10;
const long long linf = ~0ull / 20;
const int maxn = 2e3 + 20;
vector<int> adj[maxn];
bool vis[maxn];
int d[maxn];
int main() {
  iOS();
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      if (c == '#') {
        adj[i].push_back(j + n);
        adj[j + n].push_back(i);
      }
    }
  queue<int> q;
  q.push(0);
  vis[0] = 1;
  while (q.size()) {
    int t = q.front();
    q.pop();
    for (int x : adj[t])
      if (!vis[x]) {
        q.push(x);
        vis[x] = 1;
        d[x] = d[t] + 1;
      }
  }
  if (d[n - 1] == 0)
    cout << -1;
  else
    cout << d[n - 1];
  return 0;
}
