#include <bits/stdc++.h>
using namespace std;
char s[1010][1010];
int dis[1010][1010];
bool row[1010], col[1010], mark[1010][1010];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) cin >> s[i][j];
  queue<pair<int, int> > q;
  q.push(make_pair(1, 0));
  dis[1][0] = 1;
  s[n][m + 1] = '#';
  while (q.size()) {
    pair<int, int> v = q.front();
    q.pop();
    if (row[v.first] == false) {
      row[v.first] = true;
      for (int i = 1; i <= m + 1; i++)
        if (s[v.first][i] == '#' && mark[v.first][i] == false) {
          mark[v.first][i] = true;
          dis[v.first][i] = dis[v.first][v.second] + 1;
          q.push(make_pair(v.first, i));
        }
    }
    if (col[v.second] == false) {
      col[v.second] = true;
      for (int i = 1; i <= n; i++)
        if (s[i][v.second] == '#' && mark[i][v.second] == false) {
          mark[i][v.second] = true;
          dis[i][v.second] = dis[v.first][v.second] + 1;
          q.push(make_pair(i, v.second));
        }
    }
  }
  if (dis[n][m + 1] == 0)
    cout << -1 << '\n';
  else
    cout << dis[n][m + 1] - 2 << '\n';
  return 0;
}
