#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
int n, m;
char a[1005][1005];
deque<int> dq;
int dis[2005];
vector<int> adj[2005];
int main() {
  cin >> n >> m;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < m; j++) {
      a[i][j] = s[j];
      if (a[i][j] == '#') {
        adj[i].push_back(j + n);
        adj[j + n].push_back(i);
      }
    }
  }
  for (int i = 0; i < n + m; i++) dis[i] = INF;
  dis[n - 1] = 0;
  dq.push_front(n - 1);
  int v;
  while (dq.size()) {
    v = dq.back();
    dq.pop_back();
    for (int u : adj[v]) {
      if (dis[u] > dis[v] + 1) {
        dis[u] = dis[v] + 1;
        dq.push_front(u);
      }
    }
  }
  if (dis[0] == INF)
    cout << -1;
  else
    cout << dis[0];
  return 0;
}
