#include <bits/stdc++.h>
using namespace std;
vector<string> g;
struct data {
  int x;
  int y;
  int p;
  int cnt;
} u, v;
const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int f[1001][1001][4];
void display() {
  memset(f, 1, sizeof(f));
  int MAX = f[0][0][0];
  int n, m;
  cin >> n >> m;
  g.clear();
  g.resize(n);
  for (int i = 0; i < n; i++) cin >> g[i];
  queue<data> Q;
  while (!Q.empty()) Q.pop();
  u.x = u.y = u.cnt = 0;
  u.p = 3;
  f[0][0][3] = 0;
  Q.push(u);
  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    if (g[u.x][u.y] == '#') {
      for (int i = 0; i < 4; i++) {
        int dx = u.x + dir[i][0];
        int dy = u.y + dir[i][1];
        if (dx < 0 || dx >= n || dy < 0 || dy >= m) continue;
        v.x = dx;
        v.y = dy;
        v.p = i;
        v.cnt = u.cnt;
        if (v.p != u.p) v.cnt++;
        if (f[v.x][v.y][v.p] > v.cnt) {
          f[v.x][v.y][v.p] = v.cnt;
          Q.push(v);
        }
      }
    } else {
      int dx = u.x + dir[u.p][0];
      int dy = u.y + dir[u.p][1];
      if (dx < 0 || dx >= n || dy < 0 || dy >= m) continue;
      v.x = dx;
      v.y = dy;
      v.p = u.p;
      v.cnt = u.cnt;
      if (f[v.x][v.y][v.p] > v.cnt) {
        f[v.x][v.y][v.p] = v.cnt;
        Q.push(v);
      }
    }
  }
  if (g[n - 1][m - 1] != '#')
    cout << (f[n - 1][m - 1][3] < MAX ? f[n - 1][m - 1][3] : -1) << endl;
  else {
    int MIN = MAX;
    for (int i = 0; i < 4; i++)
      MIN = min(MIN, f[n - 1][m - 1][i] + (i == 3 ? 0 : 1));
    cout << (MIN < MAX ? MIN : -1) << endl;
  }
}
int main() {
  display();
  return 0;
}
