#include <bits/stdc++.h>
using namespace std;
using tint = long long;
using ld = long double;
using pi = pair<tint, int>;
using pl = pair<tint, tint>;
using vi = vector<int>;
using vpi = vector<pi>;
using vvi = vector<vi>;
using vl = vector<tint>;
using vb = vector<bool>;
const int MOD = 1e9 + 7;
const tint mod = 998244353;
const int MX = 1500;
const tint INF = 1e18;
const int inf = 2e9;
const ld PI = acos(ld(-1));
const ld eps = 1e-8;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
template <class T>
void remDup(vector<T>& v) {
  sort(begin(v), end(v));
  v.erase(unique(begin(v), end(v)), end(v));
}
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
bool valid(int x, int y, int n, int m) {
  return (0 <= x && x < n && 0 <= y && y < m);
}
int cdiv(int a, int b) { return a / b + ((a ^ b) > 0 && a % b); }
int fdiv(int a, int b) { return a / b - ((a ^ b) < 0 && a % b); }
void NACHO(string name = "") {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if ((int)(name).size()) {
    freopen((name + ".in").c_str(), "r", stdin);
    freopen((name + ".out").c_str(), "w", stdout);
  }
}
string tab[MX];
vpi adj[MX * MX + 5];
deque<int> q;
int dist[MX * MX + 5];
int main() {
  NACHO();
  int n, m;
  cin >> n >> m;
  for (int i = (0); i < (n); ++i) cin >> tab[i];
  if (tab[n - 1][m - 1] == '#')
    adj[n * m + m * (n - 1) + m - 1].push_back(
        make_pair(m * (n - 1) + m - 1, 1));
  for (int i = (0); i < (n); ++i) {
    for (int j = (0); j < (m); ++j) {
      int w = (tab[i][j] == '#');
      if (w == 1) {
        if (j < m - 1) {
          swap(n, m);
          adj[n * i + j].push_back(make_pair(n * i + j + 1, 0));
          adj[n * i + j + 1].push_back(make_pair(n * i + j, 0));
          adj[n * m + n * i + j].push_back(make_pair(n * i + j + 1, 1));
          adj[n * i + j + 1].push_back(make_pair(n * m + n * i + j, 1));
          swap(n, m);
        }
        if (i < n - 1) {
          swap(n, m);
          adj[n * m + n * i + j].push_back(
              make_pair(n * m + n * (i + 1) + j, 0));
          adj[n * m + n * (i + 1) + j].push_back(
              make_pair(n * m + n * i + j, 0));
          adj[n * i + j].push_back(make_pair(n * m + n * (i + 1) + j, 1));
          adj[n * m + n * (i + 1) + j].push_back(make_pair(n * i + j, 1));
          swap(n, m);
        }
      } else {
        if (j < m - 1) {
          swap(n, m);
          adj[n * i + j].push_back(make_pair(n * i + j + 1, 0));
          adj[n * i + j + 1].push_back(make_pair(n * i + j, 0));
          swap(n, m);
        }
        if (i < n - 1) {
          swap(n, m);
          adj[n * m + n * i + j].push_back(
              make_pair(n * m + n * (i + 1) + j, 0));
          adj[n * m + n * (i + 1) + j].push_back(
              make_pair(n * m + n * i + j, 0));
          swap(n, m);
        }
      }
    }
  }
  for (int i = (0); i < (n * m + n * m); ++i) dist[i] = inf;
  dist[0] = 0;
  q.push_front(0);
  while ((int)(q).size()) {
    auto u = q.front();
    q.pop_front();
    for (auto& v : adj[u]) {
      if (dist[u] + v.second < dist[v.first]) {
        dist[v.first] = dist[u] + v.second;
        if (v.second == 0)
          q.push_front(v.first);
        else
          q.push_back(v.first);
      }
    }
  }
  if (dist[m * (n - 1) + m - 1] == inf) dist[m * (n - 1) + m - 1] = -1;
  cout << dist[m * (n - 1) + m - 1] << "\n";
}
