#include <bits/stdc++.h>
using namespace std;
class SignedReader {
  char arr[262144];
  char *p, *e;
  int s, val;
  bool neg;
  void getTok() {
    p = arr;
    s = fread(arr, 1, 262144, stdin);
    e = arr + s;
  }
  void skip() {
    while (true) {
      if (p == e) getTok();
      if (*p == '-' || ((*p) >= '0' && (*p) <= '9')) return;
      p++;
    }
  }

 public:
  SignedReader() { getTok(); }
  int nextInt() {
    neg = false;
    skip();
    if (*p == '-') neg = true, p++;
    val = 0;
    while (true) {
      if (p == e) getTok();
      if (p == e) break;
      if (((*p) >= '0' && (*p) <= '9'))
        val = 10 * val + *p - 48, p++;
      else
        break;
    }
    if (neg) val = -val;
    return val;
  }
  long long nextLong() {
    long long value;
    neg = false;
    skip();
    if (*p == '-') neg = true, p++;
    value = 0;
    while (true) {
      if (p == e) getTok();
      if (p == e) break;
      if (((*p) >= '0' && (*p) <= '9'))
        value = 10 * value + *p - 48, p++;
      else
        break;
    }
    if (neg) value = -value;
    return value;
  }
};
char grid[1005][1005];
set<int> colOpposite[1005];
set<int> rowOpposite[1005];
int minDist[1005][1005];
int row, col;
int bfs() {
  int ux, uy, vx, vy;
  set<int>::iterator iter, iter2;
  queue<int> q;
  for (iter = rowOpposite[row].begin(); iter != rowOpposite[row].end();
       iter++) {
    q.push(row);
    q.push(*iter);
    minDist[row][*iter] = 0;
    colOpposite[*iter].erase(row);
  }
  rowOpposite[row].clear();
  while (!q.empty()) {
    ux = q.front();
    q.pop();
    uy = q.front();
    q.pop();
    if (ux == 1) return minDist[ux][uy] + 1;
    for (iter = rowOpposite[ux].begin(); iter != rowOpposite[ux].end();
         iter++) {
      vx = ux;
      vy = *iter;
      minDist[vx][vy] = minDist[ux][uy] + 1;
      q.push(vx);
      q.push(vy);
      colOpposite[vy].erase(vx);
    }
    rowOpposite[ux].clear();
    for (iter = colOpposite[uy].begin(); iter != colOpposite[uy].end();
         iter++) {
      vx = *iter;
      vy = uy;
      minDist[vx][vy] = minDist[ux][uy] + 1;
      q.push(vx);
      q.push(vy);
      rowOpposite[vx].erase(vy);
    }
    colOpposite[uy].clear();
  }
  return -1;
}
void printDistanceGrid() {
  int i, j;
  puts("");
  for (i = 1; i <= row; i++) {
    for (j = 1; j <= col; j++) {
      if (grid[i][j] == '.')
        putchar('.');
      else if (minDist[i][j] != (1 << 29))
        printf("%d", minDist[i][j]);
      else
        putchar('#');
    }
    puts("");
  }
  puts("");
}
int main() {
  scanf("%d %d", &row, &col);
  gets(grid[0]);
  int i, j;
  for (i = 1; i <= row; i++) {
    gets(grid[i] + 1);
    for (j = 1; j <= col; j++)
      if (grid[i][j] == '#') {
        colOpposite[j].insert(i);
        rowOpposite[i].insert(j);
        minDist[i][j] = (1 << 29);
      }
  }
  if (row == 1)
    puts("0");
  else if (!rowOpposite[1].size() || !rowOpposite[row].size())
    puts("-1");
  else {
    int dist = bfs();
    printf("%d\n", dist);
  }
  return 0;
}
