#include <bits/stdc++.h>
using namespace std;
int n, m, vis[2005], dist[2005];
char grid[1005][1005];
vector<int> v[2005];
queue<int> q;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> grid[i][j];
      if (grid[i][j] == '#') {
        v[i].push_back(j + 1001);
        v[j + 1001].push_back(i);
      }
    }
  }
  vis[1] = 1;
  dist[1] = 0;
  q.push(1);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (auto a : v[x]) {
      if (vis[a]) continue;
      vis[a] = 1;
      dist[a] = dist[x] + 1;
      q.push(a);
    }
  }
  if (!vis[n])
    cout << "-1\n";
  else
    cout << dist[n] << "\n";
  return 0;
}
