#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
string second[maxn];
int mark[2 * maxn], h[2 * maxn], now;
vector<int> v[2 * maxn];
queue<int> q;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> second[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (second[i][j] == '#') v[i].push_back(j + n), v[j + n].push_back(i);
  q.push(0), mark[0] = 1;
  while (q.size()) {
    now = q.front();
    q.pop();
    if (now == n - 1) return cout << h[now], 0;
    for (int i = 0; i < v[now].size(); i++)
      if (!mark[v[now][i]])
        h[v[now][i]] = h[now] + 1, mark[v[now][i]] = 1, q.push(v[now][i]);
  }
  cout << -1;
}
