#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  for (int i = 0; i < H; ++i) {
    cin >> S[i];
  }
  int sx = H - 1;
  int sy = 0;
  int gx = 0;
  int gy = W - 1;
  const int dx[4] = {1, 0, -1, 0};
  const int dy[4] = {0, 1, 0, -1};
  vector<vector<int>> dist(H, vector<int>(W, -1));
  dist[sx][sy] = 0;
  vector<vector<int>> prev_x(H, vector<int>(W, -1));
  vector<vector<int>> prev_y(H, vector<int>(W, -1));
  queue<pair<int, int>> que;
  que.push(make_pair(sx, sy));
  while (!que.empty()) {
    pair<int, int> cur = que.front();
    int x = cur.first;
    int y = cur.second;
    que.pop();
    for (int dir = 0; dir < 4; ++dir) {
      int next_x = x + dx[dir];
      int next_y = y + dy[dir];
      if (next_x < 0 || next_x >= H || next_y < 0 || next_y >= W) continue;
      if (S[next_x][next_y] == '#') continue;
      if (dist[next_x][next_y] == -1) {
        que.push(make_pair(next_x, next_y));
        dist[next_x][next_y] = dist[x][y] + 1;
        prev_x[next_x][next_y] = x;
        prev_y[next_x][next_y] = y;
      }
    }
  }
  cout << dist[gx][gy] << endl;
  return 0;
}
