#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10, inf = 1e9;
int n, m;
int dis[maxn << 1], visit[maxn << 1];
char mp[maxn][maxn];
vector<int> e[maxn << 1];
void bfs() {
  for (int i = 1; i <= n + m; ++i) dis[i] = inf;
  dis[1] = 0;
  queue<int> q;
  q.push(1);
  visit[1] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : e[u]) {
      if (visit[v]) continue;
      visit[v] = 1;
      dis[v] = dis[u] + 1;
      q.push(v);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", mp[i] + 1);
    for (int j = 1; j <= m; ++j)
      if (mp[i][j] == '#') e[i].push_back(j + n), e[j + n].push_back(i);
  }
  bfs();
  if (dis[n] == inf)
    puts("-1");
  else
    printf("%d\n", dis[n]);
  return 0;
}
