#include <bits/stdc++.h>
using namespace std;
struct point {
  int x, y;
} q[1010 * 1010 * 4];
struct node {
  int next, now;
} d[1010 * 1010 * 2];
int c_c[1010], c_r[1010];
int dis[1010 * 1010];
bool vis[1010 * 1010], use_r[1010], use_c[1010];
int n, m;
void add_edge_col(int u, int v, int i) {
  d[i].next = c_c[u];
  c_c[u] = i;
  d[i].now = v;
}
void add_edge_row(int u, int v, int i) {
  d[i].next = c_r[u];
  c_r[u] = i;
  d[i].now = v;
}
void init() {
  cin >> n >> m;
  int l = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      char ch;
      cin >> ch;
      if (ch == '#') {
        add_edge_col(j, i, ++l);
        add_edge_row(i, j, ++l);
      }
    }
  add_edge_row(n, m + 1, ++l);
}
void solve() {
  int head = 1, tail = 1;
  memset(dis, 64, sizeof(dis));
  q[1].x = 1;
  q[1].y = 0;
  dis[0] = 0;
  vis[0] = true;
  bool flag = 0;
  while (head <= tail) {
    int u = (q[head].x - 1) * m + q[head].y;
    if (dis[u] >= dis[n * m + 1]) {
      vis[u] = false;
      head++;
      continue;
    }
    if (!use_r[q[head].x])
      for (int i = c_r[q[head].x]; i; i = d[i].next) {
        int v = (q[head].x - 1) * m + d[i].now;
        if (dis[v] > dis[u] + 1) {
          if (v == n * m + 1) flag = 1;
          if (!vis[v] && v != n * m + 1) {
            tail++;
            q[tail].x = q[head].x;
            q[tail].y = d[i].now;
            vis[v] = true;
          }
          dis[v] = dis[u] + 1;
        }
      }
    use_r[q[head].x] = true;
    if (!use_c[q[head].y])
      for (int i = c_c[q[head].y]; i; i = d[i].next) {
        int v = (d[i].now - 1) * m + q[head].y;
        if (dis[v] > dis[u] + 1) {
          if (v == n * m + 1) flag = 1;
          if (!vis[v] && v != n * m + 1) {
            tail++;
            q[tail].x = d[i].now;
            q[tail].y = q[head].y;
            vis[v] = true;
          }
          dis[v] = dis[u] + 1;
        }
      }
    use_c[q[head].y] = true;
    vis[u] = false;
    head++;
  }
  if (flag)
    cout << dis[n * m + 1] - 1 << endl;
  else
    cout << "-1" << endl;
}
int main() {
  ios::sync_with_stdio(false);
  init();
  solve();
  return 0;
}
