#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, m;
  cin >> n >> m;
  char G[n][m];
  for (int i = 0; i < n; i++) scanf("%s", G[i]);
  for (int i = 0; i < m; i++)
    if (G[n - 1][i] == '#') goto work;
  cout << (-1) << endl;
  return 0;
work:
  queue<int> X, Y, dist;
  for (int j = 0; j < m; j++)
    if (G[0][j] == '#') X.push(0), Y.push(j), dist.push(1);
  set<int> visr, visc, nr, nc;
  visr.insert(0);
  while (X.size()) {
    int x = X.front(), y = Y.front(), d = dist.front();
    X.pop(), Y.pop(), dist.pop();
    if (nr.find(y) == nr.end())
      for (int i = n - 1; i >= 0; i--)
        if (G[i][y] == '#' && visr.find(i) == visr.end()) {
          G[i][y] = '.', X.push(i), Y.push(y), dist.push(d + 1), visr.insert(i);
          if (i + 1 == n) {
            cout << (d + 1) << endl;
            return 0;
          }
        }
    nr.insert(y);
    if (nc.find(x) == nc.end())
      for (int j = 0; j < m; j++)
        if (G[x][j] == '#' && visc.find(j) == visc.end())
          G[x][j] = '.', X.push(x), Y.push(j), dist.push(d + 1), visc.insert(j);
    nc.insert(x);
  }
  cout << (-1) << endl;
  return 0;
}
