#include <bits/stdc++.h>
using namespace std;
long long sub(long long a, long long b) {
  return (a - b + 1000000007) % 1000000007;
}
long long add(long long a, long long b) { return (a + b) % 1000000007; }
long long mul(long long a, long long b) { return (a * b) % 1000000007; }
int n, m;
struct Point {
  int first;
  int second;
};
struct queueNode {
  Point pt;
  int dist;
};
bool isValid(int row, int col) {
  return (row >= 0) && (row < n) && (col >= 0) && (col < m);
}
int rowNum[] = {-1, 0, 0, 1};
int colNum[] = {0, -1, 1, 0};
int BFS(int a[][21], Point src, Point dest) {
  if (!a[src.first][src.second] || !a[dest.first][dest.second]) return -1;
  bool visited[n][m];
  memset(visited, false, sizeof visited);
  visited[src.first][src.second] = true;
  queue<queueNode> q;
  queueNode s = {src, 0};
  q.push(s);
  while (!q.empty()) {
    queueNode curr = q.front();
    Point pt = curr.pt;
    if (pt.first == dest.first && pt.second == dest.second) return curr.dist;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int row = pt.first + rowNum[i];
      int col = pt.second + colNum[i];
      if (isValid(row, col) && a[row][col] && !visited[row][col]) {
        visited[row][col] = true;
        queueNode Adjcell = {{row, col}, curr.dist + 1};
        q.push(Adjcell);
      }
    }
  }
  return -1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long t = 1;
  while (t--) {
    cin >> n >> m;
    char c;
    int a[21][21];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> c;
        if (c == '.')
          a[i][j] = 1;
        else
          a[i][j] = 0;
      }
    }
    Point source = {0, 0};
    Point dest = {n - 1, m - 1};
    int dist = BFS(a, source, dest);
    source = {n - 1, 0};
    dest = {0, m - 1};
    int dist1 = BFS(a, source, dest);
    cout << max(dist, dist1);
    return 0;
  }
}
