#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  string s[n];
  for (int i = 0; i < n; i++) cin >> s[i];
  vector<vector<int>> row(n), col(m);
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '#') {
        row[i].push_back(j);
        col[j].push_back(i);
      }
    }
  }
  deque<pair<int, int>> q;
  int rw[n];
  int rvis[n];
  for (int i = 0; i < n; i++) {
    rw[i] = INT_MAX;
    rvis[i] = 0;
  }
  int cw[m];
  int cvis[m];
  for (int i = 0; i < m; i++) {
    cw[i] = INT_MAX;
    cvis[i] = 0;
  }
  rw[n - 1] = 0;
  q.push_back(make_pair(0, n - 1));
  while (q.size() > 0) {
    int type = q.front().first;
    int num = q.front().second;
    q.pop_front();
    if (type == 0) {
      if (rvis[num] == 1)
        continue;
      else
        rvis[num] = 1;
      if (num == 0) break;
      for (int i = 0; i < row[num].size(); i++) {
        int cn = row[num][i];
        if (cvis[cn] == 0 && cw[cn] > rw[num] + 1) {
          cw[cn] = rw[num] + 1;
          q.push_back(make_pair(1, cn));
        }
      }
    } else if (type == 1) {
      if (cvis[num] == 1)
        continue;
      else
        cvis[num] = 1;
      for (int i = 0; i < col[num].size(); i++) {
        int rn = col[num][i];
        if (rvis[rn] == 0 && rw[rn] > cw[num] + 1) {
          rw[rn] = cw[num] + 1;
          q.push_back(make_pair(0, rn));
        }
      }
    }
  }
  if (rw[0] == INT_MAX)
    cout << -1;
  else
    cout << rw[0];
}
