#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e6 + 1;
int n, m;
int main() {
  cin >> n >> m;
  int d[n][m], w[n][m];
  string s[n];
  int ans[n + m];
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = 0; j < m; j++) {
      d[i][j] = 0;
      w[i][j] = 0;
    }
  }
  for (int i = 0; i < m + n; i++) {
    ans[i] = 0;
  }
  if (n == 1 || m == 1) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (s[i][j] == '#') {
          cout << 0;
          return 0;
        }
      }
    }
    cout << 1;
    return 0;
  }
  d[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i == 0 && j == 0) {
        continue;
      }
      if (s[i][j] == '#') {
        continue;
      }
      d[i][j] = max((i > 0 ? d[i - 1][j] : 0), (j > 0 ? d[i][j - 1] : 0));
    }
  }
  w[n - 1][m - 1] = 1;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; j--) {
      if (i == n - 1 && j == m - 1) {
        continue;
      }
      if (s[i][j] == '#') {
        continue;
      }
      w[i][j] =
          max((i + 1 < n ? w[i + 1][j] : 0), (j + 1 < m ? w[i][j + 1] : 0));
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i + j == 0 || i + j == n + m - 2) {
        continue;
      }
      if (s[i][j] == '#') {
        continue;
      }
      if (d[i][j] && w[i][j]) {
        ans[i + j]++;
      }
    }
  }
  int ok = 2;
  for (int i = 1; i < n + m - 2; i++) {
    if (!ans[i]) {
      cout << 0;
      return 0;
    }
    if (ans[i] == 1) {
      ok = 1;
    }
  }
  cout << ok;
}
