#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
inline istream& operator>>(istream& str, pair<T, U>& p) {
  return str >> p.first >> p.second;
}
template <class T, class U>
inline ostream& operator<<(ostream& str, pair<T, U>& p) {
  return str << p.first << " " << p.second << endl;
}
template <class T>
inline istream& operator>>(istream& str, vector<T>& a) {
  for (auto& i : a) str >> i;
  return str;
}
template <class T>
inline ostream& operator<<(ostream& str, vector<T>& a) {
  for (auto& i : a) cout << i << " ";
  cout << endl;
  return str;
}
template <class T, class U>
inline ostream& operator<<(ostream& str, map<T, U>& a) {
  for (auto& i : a) cout << i.first << " " << i.second << endl;
  return str;
}
int dx[] = {0, -1};
int dy[] = {-1, 0};
long long int mods[7] = {469762049,    998244353,    1004535809,
                         (int)1e9 + 7, (int)1e9 + 9, (int)1e9 + 21,
                         (int)1e9 + 33};
struct const_hash {
  long long int val[4];
  const_hash() { memset(val, 0, sizeof(val)); }
  const_hash(int v) { fill(val, val + 4, v); }
  const_hash operator+(const const_hash& other) const {
    const_hash res;
    for (int i = 0; i < 4; i++) res.val[i] = (val[i] + other.val[i]) % mods[i];
    return res;
  }
  const_hash operator*(const const_hash& other) const {
    const_hash res;
    for (int i = 0; i < 4; i++) res.val[i] = (val[i] * other.val[i]) % mods[i];
    return res;
  }
  bool operator==(const const_hash& other) const {
    for (int i = 0; i < 4; i++)
      if (val[i] != other.val[i]) return false;
    return true;
  }
};
ostream& operator<<(ostream& str, const const_hash& node) {
  str << node.val[0];
  return str;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  shuffle(mods, mods + 7, rng);
  char mat[n + 1][m + 1];
  const_hash dp1[n + 1][m + 1], dp2[n + 1][m + 1];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> mat[i][j];
    }
  }
  dp1[0][0] = const_hash(1);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (mat[i][j] == '.') {
        if (i - 1 >= 0) dp1[i][j] = dp1[i][j] + dp1[i - 1][j];
        if (j - 1 >= 0) dp1[i][j] = dp1[i][j] + dp1[i][j - 1];
      }
    }
  }
  dp2[n - 1][m - 1] = const_hash(1);
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; j--) {
      if (mat[i][j] == '.') {
        if (i + 1 < n) dp2[i][j] = dp2[i][j] + dp2[i + 1][j];
        if (j + 1 < m) dp2[i][j] = dp2[i][j] + dp2[i][j + 1];
      }
    }
  }
  int ans = 2;
  if (dp1[n - 1][m - 1] == const_hash(0)) ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (mat[i][j] == '.' and (!(i == 0 and j == 0)) and
          (!(i == n - 1 and j == m - 1))) {
        if (dp1[n - 1][m - 1] == dp1[i][j] * dp2[i][j]) ans = min(ans, 1);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
