#include <bits/stdc++.h>
#pragma comment(linker, "/stack:64000000")
using namespace std;
template <typename first>
inline first abs(const first& a) {
  return a < 0 ? -a : a;
}
template <typename first>
inline first sqr(const first& a) {
  return a * a;
}
const long double PI = 3.1415926535897932384626433832795;
const long double EPS = 1e-9;
const int INF = 1000 * 1000 * 1000;
const int N = 1000 + 3;
int n, m;
char f[N][N];
int d[N][N][4];
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < int(n); i++) scanf("%s", f[i]);
  memset(d, 63, sizeof(d));
  d[n - 1][m - 1][0] = 0;
  queue<pair<pair<int, int>, int> > q;
  q.push(make_pair((make_pair((n - 1), (m - 1))), (0)));
  while (!q.empty()) {
    pair<int, int> cur = q.front().first;
    int dir = q.front().second;
    q.pop();
    int x = cur.first, y = cur.second;
    int dv = d[x][y][dir];
    {
      int tx = x + dx[dir], ty = y + dy[dir];
      if ((0 <= (tx) && (tx) < (n) && 0 <= (ty) && (ty) < (m)) &&
          d[tx][ty][dir] > dv) {
        d[tx][ty][dir] = dv;
        q.push(make_pair((make_pair((tx), (ty))), (dir)));
      }
    }
    if (f[x][y] == '#') {
      for (int i = 0; i < int(4); i++)
        if (d[x][y][i] > dv + 1) {
          d[x][y][i] = dv + 1;
          q.push(make_pair((make_pair((x), (y))), (i)));
        }
    }
  }
  if (d[0][0][0] > INF / 2)
    puts("-1");
  else
    cout << d[0][0][0] << endl;
  return 0;
}
