#include <bits/stdc++.h>
using namespace std;
struct state {
  int x, y, d;
  state(int x, int y, int d) : x(x), y(y), d(d) {}
};
const int N = 1e3;
deque<state> qu;
int vis[N][N][4];
int cost[N][N][4];
char g[N][N + 1];
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) scanf("%s", g[i]);
  qu.push_front(state(n - 1, m - 1, 1));
  memset(cost, 0x3f, sizeof cost);
  cost[n - 1][m - 1][1] = 0;
  int res = 1e9;
  while (!qu.empty()) {
    state e = qu.front();
    qu.pop_front();
    if (e.x == 0 && e.d == 1) {
      res = cost[e.x][e.y][e.d];
      break;
    }
    if (e.x == 0 && g[e.x][e.y] == '#') {
      res = min(res, cost[e.x][e.y][e.d] + 1);
      continue;
    }
    if (vis[e.x][e.y][e.d]) continue;
    int curCost = cost[e.x][e.y][e.d];
    vis[e.x][e.y][e.d] = true;
    if (g[e.x][e.y] == '.') {
      int tx = e.x + dx[e.d];
      int ty = e.y + dy[e.d];
      if (tx < 0 || ty < 0 || tx >= n || ty >= m) continue;
      if (vis[tx][ty][e.d] || curCost >= cost[tx][ty][e.d]) continue;
      cost[tx][ty][e.d] = curCost;
      qu.push_front(state(tx, ty, e.d));
      continue;
    }
    for (int i = 0; i < 4; ++i) {
      int tx = e.x + dx[i];
      int ty = e.y + dy[i];
      int newCost = curCost + (i != e.d);
      if (tx < 0 || ty < 0 || tx >= n || ty >= m) continue;
      if (vis[tx][ty][i] || newCost >= cost[tx][ty][i]) continue;
      cost[tx][ty][i] = newCost;
      if (newCost == curCost)
        qu.push_front(state(tx, ty, i));
      else
        qu.push_back(state(tx, ty, i));
    }
  }
  if (res == 1e9) res = -1;
  printf("%d\n", res);
  return 0;
}
