#include <bits/stdc++.h>
using namespace std;
enum { UP, DOWN, LEFT, RIGHT };
const int MaxN = 1005;
const int MaxM = MaxN * MaxN;
const int oo = 0x3F3F3F3F;
char mp[MaxN][MaxN];
int dir[MaxM][4];
int d[MaxM][4];
int id[MaxN][MaxN], K;
int R, C, s, t;
struct Node {
  int u, dir, d;
  Node() {}
  Node(int u, int dir, int d) : u(u), dir(dir), d(d) {}
};
void BFS() {
  deque<Node> q;
  Node u, v;
  memset(d, 0x3F, sizeof(d));
  q.push_back(Node(s, RIGHT, 0));
  d[s][RIGHT] = 0;
  while (!q.empty()) {
    u = q.front();
    q.pop_front();
    if (d[u.u][u.dir] != u.d) continue;
    for (int i = 0; i < 4; i++) {
      if (dir[u.u][i]) {
        v.u = dir[u.u][i];
        v.dir = i;
        v.d = u.d + (i != u.dir);
        if (d[v.u][v.dir] > v.d) {
          d[v.u][v.dir] = v.d;
          if (i != u.dir)
            q.push_back(v);
          else
            q.push_front(v);
        }
      }
    }
  }
}
int main() {
  while (scanf("%d%d", &R, &C) == 2) {
    for (int i = 0; i < R; i++) {
      scanf("%s", mp[i]);
      for (int j = 0; j < C; j++) {
        if (mp[i][j] == '#') {
          id[i][j] = ++K;
        } else {
          id[i][j] = 0;
        }
      }
    }
    s = ++K;
    t = ++K;
    fill(dir[s], dir[s] + 4, 0);
    fill(dir[t], dir[t] + 4, 0);
    for (int i = 0; i < R; i++) {
      int last = i == 0 ? s : 0;
      for (int j = 0; j < C; j++) {
        if (mp[i][j] == '#') {
          if (i == 0 && dir[s][RIGHT] == 0) dir[s][RIGHT] = id[i][j];
          dir[id[i][j]][LEFT] = last;
          last = id[i][j];
        }
      }
      last = i == R - 1 ? t : 0;
      for (int j = C - 1; j >= 0; j--) {
        if (mp[i][j] == '#') {
          if (i == R - 1 && dir[t][LEFT] == 0) dir[t][LEFT] = id[i][j];
          dir[id[i][j]][RIGHT] = last;
          last = id[i][j];
        }
      }
    }
    for (int j = 0; j < C; j++) {
      int last = 0;
      for (int i = 0; i < R; i++) {
        if (mp[i][j] == '#') {
          dir[id[i][j]][UP] = last;
          last = id[i][j];
        }
      }
      last = 0;
      for (int i = R - 1; i >= 0; i--) {
        if (mp[i][j] == '#') {
          dir[id[i][j]][DOWN] = last;
          last = id[i][j];
        }
      }
    }
    BFS();
    if (d[t][RIGHT] == oo)
      puts("-1");
    else
      printf("%d\n", d[t][RIGHT]);
  }
  return 0;
}
