#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
int r[1010], c[1010];
queue<pair<char, int> > q;
vector<int> rr[1010], cc[1010];
char s[1010];
int main() {
  int n, m;
  for (int(i) = 0; (i) < (1005); ++(i)) r[i] = c[i] = -1;
  cin >> n >> m;
  r[n - 1] = 0;
  for (int(i) = 0; (i) < (n); ++(i)) {
    scanf("%s", s);
    for (int(j) = 0; (j) < (m); ++(j))
      if (s[j] == '#') {
        rr[i].push_back(j);
        cc[j].push_back(i);
      }
  }
  q.push(make_pair(('r'), (n - 1)));
  int cur;
  while (!q.empty() && r[0] == -1) {
    cur = q.front().second;
    if (q.front().first == 'r') {
      for (int(i) = 0; (i) < int((rr[cur]).size()); ++(i))
        if (c[rr[cur][i]] == -1) {
          c[rr[cur][i]] = r[cur] + 1;
          q.push(make_pair(('c'), (rr[cur][i])));
        }
    }
    if (q.front().first == 'c') {
      for (int(i) = 0; (i) < int((cc[cur]).size()); ++(i))
        if (r[cc[cur][i]] == -1) {
          r[cc[cur][i]] = c[cur] + 1;
          q.push(make_pair(('r'), (cc[cur][i])));
        }
    }
    q.pop();
  }
  printf("%d\n", r[0]);
}
