#include <bits/stdc++.h>
using namespace std;
vector<int> R[1003];
vector<int> C[1003];
char L[1003];
int r, c;
int mark[1003][1003], distA[1003][1003], colaC[1003 * 1003], colaR[1003 * 1003],
    markR[1003], markC[1003];
int total, primero, ultimo, nodr, nodc;
void DoBFS() {
  for (int p = (1); p <= (r); p += (1))
    for (int q = (1); q <= (c); q += (1)) distA[p][q] = 1e9;
  for (int p = (1); p <= (r); p += (1))
    for (int q = (1); q <= (c); q += (1)) mark[p][q] = 0;
  total = ultimo = primero = 0;
  for (int p = (1); p <= (r); p += (1)) markR[p] = 0;
  for (int q = (1); q <= (c); q += (1)) markC[q] = 0;
  markR[r] = 1;
  int large = R[r].size() - 1;
  for (int p = (0); p <= (large); p += (1)) {
    total++;
    ultimo++;
    colaR[ultimo] = r;
    colaC[ultimo] = R[r][p];
    mark[r][R[r][p]] = distA[r][R[r][p]] = 1;
  }
  if (total > 0) primero = 1;
  while (total > 0) {
    nodr = colaR[primero];
    nodc = colaC[primero];
    primero++;
    total--;
    if (nodr == 1) return;
    if (!markR[nodr]) {
      markR[nodr] = 1;
      int L = R[nodr].size() - 1;
      for (int p = (0); p <= (L); p += (1))
        if (0 == mark[nodr][R[nodr][p]]) {
          total++;
          ultimo++;
          colaR[ultimo] = nodr;
          colaC[ultimo] = R[nodr][p];
          mark[nodr][R[nodr][p]] = 1;
          distA[nodr][R[nodr][p]] = distA[nodr][nodc] + 1;
        }
    }
    if (!markC[nodc]) {
      markC[nodc] = 1;
      int L = C[nodc].size() - 1;
      for (int p = (0); p <= (L); p += (1))
        if (0 == mark[C[nodc][p]][nodc]) {
          total++;
          ultimo++;
          colaC[ultimo] = nodc;
          colaR[ultimo] = C[nodc][p];
          mark[C[nodc][p]][nodc] = 1;
          distA[C[nodc][p]][nodc] = distA[nodr][nodc] + 1;
        }
    }
  }
}
int main() {
  scanf("%d%d", &r, &c);
  for (int p = (1); p <= (r); p += (1)) {
    scanf("%s", &L);
    for (int q = (1); q <= (c); q += (1))
      if (L[q - 1] == '#') {
        R[p].push_back(q);
        C[q].push_back(p);
      }
  }
  DoBFS();
  int mn = 1e9;
  for (int q = (1); q <= (c); q += (1))
    if (mn > distA[1][q]) mn = distA[1][q];
  if (mn == 1e9)
    printf("-1");
  else
    printf("%d", mn);
}
