#include <bits/stdc++.h>
using namespace std;
const int M = 1020;
const int Q = 1e9 + 7;
int d[2][M];
char s[M];
bool c[M][M];
vector<int> hor[M], ver[M];
pair<int, int> q[2 * M];
int main() {
  srand(time(NULL));
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    for (int j = 0; j < m; j++)
      if (s[j] == '#') {
        hor[i].push_back(j);
        ver[j].push_back(i);
        c[i][j] = true;
      }
  }
  for (int i = 0; i < max(n, m); i++)
    for (int tq = 0; tq < 2; tq++) d[tq][i] = Q;
  int r = 0, l = 0;
  q[r++] = make_pair(0, 0);
  d[0][0] = 0;
  while (r != l) {
    pair<int, int> cur = q[l++];
    if (cur.first == 0) {
      for (int i = 0; i < (int)hor[cur.second].size(); i++) {
        if (d[1][hor[cur.second][i]] > d[cur.first][cur.second] + 1) {
          d[1][hor[cur.second][i]] = d[cur.first][cur.second] + 1;
          q[r++] = make_pair(1, hor[cur.second][i]);
        }
      }
    } else {
      for (int i = 0; i < (int)ver[cur.second].size(); i++) {
        if (d[0][ver[cur.second][i]] > d[cur.first][cur.second] + 1) {
          d[0][ver[cur.second][i]] = d[cur.first][cur.second] + 1;
          q[r++] = make_pair(0, ver[cur.second][i]);
        }
      }
    }
  }
  cout << ((d[0][n - 1] == Q) ? -1 : d[0][n - 1]);
  return 0;
}
