#include <bits/stdc++.h>
using namespace std;
inline void fileio(const char* in, const char* out) {
  freopen(in, "r", stdin);
  freopen(out, "w", stdout);
}
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<ll> VL;
const char nl = '\n';
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld EPS = 1e-9, PI = acos(-1);
int n, m;
int main() {
  atexit([]() { cerr << "Time: " << (ld)clock() / CLOCKS_PER_SEC << nl; });
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  vector<vector<char>> grid(n, vector<char>(m));
  int cnt = 0;
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (m); ++j) cin >> grid[i][j], cnt += (grid[i][j] == '#');
  if (n == 1 || m == 1) {
    cout << !cnt << nl;
    return 0;
  }
  vector<VB> reach(n + 1, VB(m + 1, false));
  reach[1][1] = true;
  for (int i = 0; i < (n); ++i) {
    for (int j = 0; j < (m); ++j) {
      if (grid[i][j] == '#') continue;
      reach[i + 1][j + 1] =
          reach[i + 1][j + 1] || (reach[i][j + 1] || reach[i + 1][j]);
    }
  }
  if (!reach[n][m]) {
    cout << 0 << nl;
    return 0;
  }
  vector<VB> revreach(n + 2, VB(m + 2, false));
  revreach[n][m] = true;
  for (int i = (n)-1; i >= 0; --i)
    for (int j = (m)-1; j >= 0; --j) {
      if (grid[i][j] == '#') continue;
      revreach[i + 1][j + 1] =
          revreach[i + 1][j + 1] ||
          (revreach[i + 2][j + 1] || revreach[i + 1][j + 2]);
    }
  VI dist(n + m + 3);
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (m); ++j) {
      dist[i + j] += (reach[i + 1][j + 1] && revreach[i + 1][j + 1]);
    }
  int ans = 2;
  for (int i = 1; i < n + m - 2; i++) {
    if (dist[i] == 1) {
      ans = 1;
      break;
    }
  }
  cout << ans << nl;
  return 0;
}
