#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int n_ = 1e5 + 1000;
long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); }
long long power(long long a, long long n) {
  long long p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
    }
    n >>= 1;
    a *= a;
  }
  return p;
}
long long power(long long a, long long n, long long mod) {
  long long p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
      p %= mod;
    }
    n >>= 1;
    a *= a;
    a %= mod;
  }
  return p % mod;
}
int n, m, dp[n_];
string s;
vector<int> e[n_];
int getId(int f, int x, int y) { return (f ? y : 1 + m + x); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int(x) = 1; (x) <= (n); (x)++) {
    cin >> s;
    for (int(j) = 0; (j) < (m); (j)++) {
      if (s[j] == '#') {
        int y = j + 1;
        e[getId(0, x, y)].push_back(getId(1, x, y));
        e[getId(1, x, y)].push_back(getId(0, x, y));
      }
    }
  }
  e[getId(0, 1, 0)].push_back(getId(1, 1, 0));
  queue<int> q;
  memset(dp, INF, sizeof(dp));
  q.push(getId(0, 1, 0));
  dp[getId(0, 1, 0)] = 0;
  while ((int)(q).size()) {
    auto u = q.front();
    q.pop();
    for (auto v : e[u]) {
      if (dp[v] > dp[u] + 1) {
        dp[v] = dp[u] + 1;
        q.push(v);
      }
    }
  }
  cout << (dp[getId(0, n, m)] == INF ? -1 : dp[getId(0, n, m)]) << "\n";
  return 0;
}
