#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int maxn = 1e6 + 10;
const int inf = 1e9 + 7;
inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
char a[1001][1001];
vector<int> g[maxn];
int n, m;
int vis[maxn];
int d[maxn];
void bfs() {
  queue<int> q;
  vis[n - 1] = 1;
  d[n - 1] = 0;
  q.push(n - 1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < g[u].size(); i++) {
      int v = g[u][i];
      if (!vis[v]) {
        vis[v] = 1;
        q.push(v);
        d[v] = d[u] + 1;
      }
    }
  }
}
int main() {
  n = read();
  m = read();
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (a[i][j] == '#') {
        g[i].push_back(j + n);
        g[j + n].push_back(i);
      }
  bfs();
  if (d[0])
    cout << d[0] << endl;
  else
    cout << "-1" << endl;
  return 0;
}
