#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int INF = INT_MAX;
const int SIZE = 100;
vector<vector<int> > d(SIZE, vector<int>(SIZE, INF));
int h, w;
int sx, sy;
int gx, gy;
const int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
vector<string> maze(SIZE);
int bfs() {
  queue<pair<int, int> > q;
  q.push(make_pair(sx, sy));
  d[sx][sy] = 0;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    if (p.first == gx && p.second == gy) break;
    for (long long(i) = 0; (i) < (4); (i)++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] == '.' &&
          d[nx][ny] == INF) {
        q.push(make_pair(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  return d[gx][gy];
}
int main() {
  cin >> h >> w;
  int count = 0;
  for (long long(i) = 0; (i) < (h); (i)++) {
    cin >> maze[i];
    for (long long(j) = 0; (j) < (w); (j)++) {
      if (maze[i][j] == '.' && count == 0) {
        count++;
        sx = i;
        sy = j;
      } else if (maze[i][j] == '.' && count == 1) {
        count++;
        gx = i;
        gy = j;
      }
    }
  }
  int res = bfs();
  cout << res << endl;
}
