#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  char c[n][m];
  for (int i = 0; i < n; i++) scanf("%s", c[i]);
  vector<vector<int>> row(n), col(m);
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < m; j++) {
      if (c[i][j] == '#') {
        row[i].push_back(j);
        col[j].push_back(i);
      }
    }
  }
  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;
  set<pair<int, pair<int, int>>> s;
  for (int i = 0; i < n; i++) {
    s.insert(make_pair(rw[i], make_pair(0, i)));
  }
  for (int i = 0; i < m; i++) {
    s.insert(make_pair(cw[i], make_pair(1, i)));
  }
  while (s.size() > 0) {
    while (s.size() > 0) {
      int t = s.begin()->second.first;
      int num = s.begin()->second.second;
      if (t == 0) {
        if (rvis[num] == 1) {
          s.erase(s.begin());
          continue;
        } else
          break;
      } else if (t == 1) {
        if (cvis[num] == 1) {
          s.erase(s.begin());
          continue;
        } else
          break;
      }
    }
    if (s.size() == 0) break;
    int t = s.begin()->second.first;
    int num = s.begin()->second.second;
    if (t == 0) {
      rvis[num] = 1;
      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;
          s.insert(make_pair(cw[cn], make_pair(1, cn)));
        }
      }
    } else if (t == 1) {
      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;
          s.insert(make_pair(rw[rn], make_pair(0, rn)));
        }
      }
    }
  }
  if (rw[0] == INT_MAX)
    cout << -1;
  else
    cout << rw[0];
}
