#include <bits/stdc++.h>
using namespace std;
inline int random(const int a, const int b) {
  return (rand() % (b - a + 1)) + a;
}
const double pi = acos(-1.);
const double eps = 1e-7;
const int inf = 2147483647;
const int _inf = -2147483648;
const long long int INF = 1e18;
const int mod = 1e9 + 7;
const int maxn = 1000;
void delay(int sec);
class Grid {
 public:
  int n, m;
  vector<string> grid;
  vector<vector<int>> cost[2];
  const int dr[4] = {0, 0, -1, 1};
  const int dc[4] = {1, -1, 0, 0};
  Grid() {
    cin >> n >> m;
    grid.resize(n);
    cost[0].assign(n, vector<int>(m, inf));
    cost[1].assign(n, vector<int>(m, inf));
  }
  bool valid(int r, int c) { return r >= 0 && r < n && c >= 0 && c < m; }
};
void solution(void) {
  Grid g;
  for (int i = 0; i < g.n; ++i) cin >> g.grid[i];
  deque<tuple<bool, int, int>> dq;
  g.cost[1][0][0] = 0;
  dq.push_front(make_tuple(true, 0, 0));
  while (!dq.empty()) {
    tuple<bool, int, int> u = dq.front();
    dq.pop_front();
    bool dir = get<0>(u);
    int r = get<1>(u);
    int c = get<2>(u);
    for (int i = 0; i < 4; ++i) {
      if (dir) {
        if (i > 1) break;
      } else if (i < 2)
        continue;
      tuple<bool, int, int> v;
      int r_to = r + g.dr[i];
      int c_to = c + g.dc[i];
      if (g.valid(r_to, c_to)) {
        if (g.cost[dir][r_to][c_to] > g.cost[dir][r][c]) {
          g.cost[dir][r_to][c_to] = g.cost[dir][r][c];
          dq.push_front(make_tuple(dir, r_to, c_to));
        }
      }
    }
    if (g.grid[r][c] == '#') {
      bool dir_to = dir ^ true;
      for (int i = 0; i < 4; ++i) {
        if (dir_to) {
          if (i > 1) break;
        } else if (i < 2)
          continue;
        tuple<bool, int, int> v;
        int r_to = r + g.dr[i];
        int c_to = c + g.dc[i];
        if (g.valid(r_to, c_to) &&
            g.cost[dir_to][r_to][c_to] > g.cost[dir][r][c] + 1) {
          g.cost[dir_to][r_to][c_to] = g.cost[dir][r][c] + 1;
          dq.push_back(make_tuple(dir_to, r_to, c_to));
        }
      }
    }
  }
  g.cost[1][g.n - 1][g.m - 1] == inf
      ? cout << "-1\n"
      : cout << g.cost[1][g.n - 1][g.m - 1] << "\n";
  return;
}
int main(int argc, char *arv[]) {
  int t = 1;
  while (t--) {
    solution();
  }
  return 0;
}
void delay(int sec) {
  int mili_sec = 1000 * sec;
  clock_t start_time = clock();
  while (clock() < start_time + mili_sec)
    ;
  return;
}
