#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e3 + 7, INF = 1e9 + 7;
int a[MAXN][MAXN], cost[MAXN][MAXN][4];
int Move[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int n, m;
char s[MAXN];
struct node {
  int x, y, dir, cost;
  node(int _1 = 0, int _2 = 0, int _3 = 0, int _4 = 0)
      : x(_1), y(_2), dir(_3), cost(_4) {}
};
deque<node> q;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= m; j++) {
      if (s[j] == '.')
        a[i][j] = 0;
      else
        a[i][j] = 1;
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      for (int k = 0; k <= 3; k++) cost[i][j][k] = INF;
  q.push_front(node(n, m, 2, 0));
  while (!q.empty()) {
    node t = q.front();
    q.pop_front();
    if (t.cost >= cost[t.x][t.y][t.dir]) continue;
    cost[t.x][t.y][t.dir] = t.cost;
    int nx = t.x + Move[t.dir][0], ny = t.y + Move[t.dir][1];
    if (1 <= nx && nx <= n && 1 <= ny && ny <= m)
      q.push_back(node(nx, ny, t.dir, t.cost));
    if (a[t.x][t.y]) {
      for (int i = 0; i <= 3; i++) {
        if (i == t.dir) continue;
        q.push_back(node(t.x, t.y, i, t.cost + 1));
      }
    }
  }
  printf("%d\n", cost[1][1][2] == INF ? -1 : cost[1][1][2]);
}
