#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int height[2 * maxn];
bool mark[2 * maxn];
string table[maxn];
queue<int> q;
vector<int> N[2 * maxn];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> table[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (table[i][j] == '#') {
        N[i].push_back(maxn + j);
        N[maxn + j].push_back(i);
      }
  q.push(n - 1);
  mark[n - 1] = 1;
  while (q.size()) {
    int v = q.front();
    q.pop();
    for (auto u : N[v]) {
      if (mark[u]) continue;
      mark[u] = 1;
      height[u] = height[v] + 1;
      q.push(u);
    }
  }
  if (height[0])
    cout << height[0] << '\n';
  else
    cout << -1 << '\n';
  return 0;
}
