#include <bits/stdc++.h>
using namespace std;
struct node {
  int r, c, steps;
  bool dir;
};
char grid[1000][1001];
bool vis[1000][1000];
deque<node> Q;
int main() {
  int n, m, i, j;
  node temp, cur;
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++) scanf("%s", grid[i]);
  for (j = m - 1; j >= 0; j--)
    if (grid[n - 1][j] == '#') {
      temp.r = n - 1;
      temp.c = j;
      temp.steps = 1;
      temp.dir = 1;
      Q.push_back(temp);
    }
  while (!Q.empty()) {
    cur = Q.front();
    Q.pop_front();
    if (cur.r == 0 && cur.dir == 0) {
      printf("%d\n", cur.steps);
      return 0;
    }
    if (vis[cur.r][cur.c]) continue;
    vis[cur.r][cur.c] = 1;
    for (i = cur.r + 1; i < n; i++)
      if (grid[i][cur.c] == '#') {
        temp.r = i;
        temp.c = cur.c;
        temp.steps = cur.steps + (cur.dir == 1);
        temp.dir = 0;
        if (cur.dir == 1)
          Q.push_back(temp);
        else
          Q.push_front(temp);
        break;
      }
    for (i = cur.r - 1; i >= 0; i--)
      if (grid[i][cur.c] == '#') {
        temp.r = i;
        temp.c = cur.c;
        temp.steps = cur.steps + (cur.dir == 1);
        temp.dir = 0;
        if (cur.dir == 1)
          Q.push_back(temp);
        else
          Q.push_front(temp);
        break;
      }
    for (i = cur.c + 1; i < m; i++)
      if (grid[cur.r][i] == '#') {
        temp.r = cur.r;
        temp.c = i;
        temp.steps = cur.steps + (cur.dir == 0);
        temp.dir = 1;
        if (cur.dir == 0)
          Q.push_back(temp);
        else
          Q.push_front(temp);
        break;
      }
    for (i = cur.c - 1; i >= 0; i--)
      if (grid[cur.r][i] == '#') {
        temp.r = cur.r;
        temp.c = i;
        temp.steps = cur.steps + (cur.dir == 0);
        temp.dir = 1;
        if (cur.dir == 0)
          Q.push_back(temp);
        else
          Q.push_front(temp);
        break;
      }
  }
  printf("-1\n");
  return 0;
}
