#include <bits/stdc++.h>
using namespace std;
char str[1002];
long long int dis[2002];
map<pair<bool, int>, int> cnt;
vector<int> graph[2002];
queue<int> q;
void bfs(int start, int end) {
  dis[start] = 0;
  q.push(start);
  while (!q.empty() && dis[end] >= 1e18) {
    int par = q.front();
    q.pop();
    for (auto child : graph[par]) {
      if (dis[child] > dis[par] + 1) {
        dis[child] = dis[par] + 1;
        q.push(child);
      }
    }
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int val = 1;
  for (int i = 0; i < n; i++) {
    scanf("%s", str);
    for (int j = 0; j < m; j++) {
      if (str[j] == '#') {
        if (cnt.count({0, i}) == 0) cnt[{0, i}] = val++;
        if (cnt.count({1, j}) == 0) cnt[{1, j}] = val++;
        graph[cnt[{0, i}]].push_back(cnt[{1, j}]);
        graph[cnt[{1, j}]].push_back(cnt[{0, i}]);
      }
    }
  }
  for (int i = 0; i < val; i++) dis[i] = 1e18;
  if (cnt.count({0, 0}) && cnt.count({0, n - 1}))
    bfs(cnt[{0, 0}], cnt[{0, n - 1}]);
  long long int ans = dis[cnt[{0, n - 1}]];
  if (ans >= 1e18) ans = -1;
  cout << ans << "\n";
  return 0;
}
