#include <bits/stdc++.h>
using namespace std;
const int N = 1050;
vector<int> v[N * 2];
queue<int> q;
char mp[1010][1010];
int vis[N * 2], d[2 * N];
void bfs() {
  d[0] = 0;
  q.push(0);
  vis[0] = 1;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < v[x].size(); ++i) {
      int y = v[x][i];
      if (vis[y]) continue;
      vis[y] = 1;
      d[y] = d[x] + 1;
      q.push(y);
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      cin >> mp[i][j];
      if (mp[i][j] == '#') {
        v[i].push_back(j + N);
        v[j + N].push_back(i);
      }
    }
  bfs();
  if (!vis[n - 1])
    cout << -1 << endl;
  else
    cout << d[n - 1] << endl;
  return 0;
}
