#include <bits/stdc++.h>
using namespace std;
int n;
int m;
vector<vector<int>> row;
vector<vector<int>> col;
vector<string> g;
vector<vector<bool>> vis;
struct node {
  int d;
  int i;
  int j;
};
struct NODE {
  vector<int> e;
  bool vis = false;
};
vector<NODE> G;
bool operator<(const node a, const node b) { return a.d > b.d; }
int dijk() {
  priority_queue<pair<int, int>> q;
  q.push({-1, n - 1});
  pair<int, int> cur;
  while (!q.empty()) {
    cur = q.top();
    q.pop();
    if (!G[cur.second].vis) {
      G[cur.second].vis = true;
      if (cur.second == 0) {
        return -cur.first - 1;
      }
      for (int i = 0; i < G[cur.second].e.size(); ++i) {
        int nex = G[cur.second].e[i];
        q.push({cur.first - 1, nex});
      }
    }
  }
  return -1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  g.resize(n);
  row.resize(n);
  col.resize(m);
  string T1(n, '.');
  string T2(m, '#');
  vis.resize(n, (vector<bool>(m, false)));
  G.resize(n + m);
  for (int i = 0; i < n; ++i) {
    cin >> g[i];
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (g[i][j] == '#') {
        G[i].e.push_back(n + j);
        G[n + j].e.push_back(i);
      }
    }
  }
  cout << dijk() << "\n";
}
