#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
struct edge {
  long long v, w, next;
} e[2000005];
long long top = 1;
long long head[2000005];
void Add_Edge(long long u, long long v, long long w) {
  e[++top] = edge{v, w, head[u]};
  head[u] = top;
  e[++top] = edge{u, w, head[v]};
  head[v] = top;
}
long long n, m;
long long dis[2000005];
bool vis[2000005];
void Spfa(long long start, long long *Head, edge *E) {
  for (register long long i = 1; i <= n + m; ++i) dis[i] = 0x7f7f7f7f7f;
  memset(vis, 0, sizeof(vis));
  dis[start] = 0;
  priority_queue<pair<long long, long long>,
                 vector<pair<long long, long long> >,
                 greater<pair<long long, long long> > >
      q;
  while (!q.empty()) q.pop();
  q.push(make_pair(dis[start], start));
  while (!q.empty()) {
    pair<long long, long long> now = q.top();
    q.pop();
    long long di = now.first, u = now.second;
    if (vis[u]) continue;
    vis[u] = 1;
    for (register long long i = Head[u]; i; i = E[i].next) {
      long long v = E[i].v, w = E[i].w;
      if (dis[v] > di + w) {
        dis[v] = di + w;
        if (!vis[v]) q.push(make_pair(dis[v], v));
      }
    }
  }
}
long long read() {
  long long x = 0;
  char c = getchar();
  long long f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -f;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return x * f;
}
void write(long long x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
signed main() {
  n = read(), m = read();
  for (register long long i = 1; i <= n; ++i) {
    char s[1005] = {};
    scanf("%s", s);
    for (register long long j = 1; j <= m; ++j)
      if (s[j - 1] == '#') Add_Edge(i, n + j, 1);
  }
  Spfa(1, head, e);
  write(dis[n] == 0x7f7f7f7f7f ? -1 : dis[n]), putchar('\n');
  return 0;
}
