#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y, dr;
} q[1111111];
int ans[1111][1111][5];
int n, m, pos, posend, nowxx, nowyy;
char ma[1111][1111];
int xx[5], yy[5];
int main() {
  xx[0] = 1;
  xx[1] = -1;
  xx[2] = 0;
  xx[3] = 0;
  yy[0] = 0;
  yy[1] = 0;
  yy[2] = 1;
  yy[3] = -1;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < str.length(); j++) ma[i][j + 1] = str[j];
  }
  deque<node> q;
  memset(ans, 0x7F, sizeof(ans));
  ans[n][m][3] = 0;
  node hehe;
  hehe.x = n;
  hehe.y = m;
  hehe.dr = 3;
  q.push_back(hehe);
  while (!q.empty()) {
    node now = q.front();
    q.pop_front();
    if (ma[now.x][now.y] == '#') {
      for (int i = 0; i <= 3; i++)
        if (ans[now.x][now.y][i] > ans[now.x][now.y][now.dr] + 1) {
          ans[now.x][now.y][i] = ans[now.x][now.y][now.dr] + 1;
          node hehe;
          hehe.x = now.x;
          hehe.y = now.y;
          hehe.dr = i;
          q.push_back(hehe);
        }
    }
    nowxx = now.x + xx[now.dr];
    nowyy = now.y + yy[now.dr];
    if (nowxx > 0 && nowxx <= n && nowyy > 0 && nowyy <= m &&
        ans[nowxx][nowyy][now.dr] > ans[now.x][now.y][now.dr]) {
      ans[nowxx][nowyy][now.dr] = ans[now.x][now.y][now.dr];
      node hehe;
      hehe.x = nowxx;
      hehe.y = nowyy;
      hehe.dr = now.dr;
      q.push_front(hehe);
    }
  }
  if (ans[1][1][3] <= 99999999)
    cout << ans[1][1][3] << endl;
  else
    cout << "-1" << endl;
}
