#include <bits/stdc++.h>
using namespace std;
const int maxn = 2005;
vector<int> edges[maxn];
string s[maxn];
int dis[maxn], vis[maxn];
int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '#') {
        edges[i].push_back(n + j);
        edges[n + j].push_back(i);
      }
    }
  }
  memset(dis, 63, sizeof dis);
  dis[n - 1] = 0;
  queue<int> q;
  q.push(n - 1);
  vis[n - 1] = 1;
  while (!q.empty()) {
    int ss = q.front();
    q.pop();
    for (auto i : edges[ss]) {
      if (!vis[i]) {
        if (dis[i] > dis[ss] + 1) {
          dis[i] = dis[ss] + 1;
          vis[i] = 1;
          q.push(i);
        }
      }
    }
  }
  cout << (dis[0] > n + m ? -1 : dis[0]) << endl;
  return 0;
}
