#include <bits/stdc++.h>
using namespace std;
vector<int> v[1001 * 2];
int n, m, vis[1001 * 2], dis[1001 * 2];
char grid[1001][1001];
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);
      }
    }
  }
  queue<int> q;
  q.push(1);
  vis[1] = 1;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < v[x].size(); ++i) {
      int a = v[x][i];
      if (!vis[a]) {
        vis[a] = 1;
        q.push(a);
        dis[a] = dis[x] + 1;
      }
    }
  }
  if (vis[n])
    cout << dis[n] << endl;
  else
    puts("-1");
  return 0;
}
