#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &u) {
  u = 0;
  char c = getchar();
  int flag = 0;
  while (c < '0' || c > '9') flag |= (c == '-'), c = getchar();
  while (c >= '0' && c <= '9')
    u = (u << 3) + (u << 1) + (c ^ 48), c = getchar();
  if (flag) u = -u;
}
bool checkmin(int &a, int b) { return (b < a ? a = b, 1 : 0); }
bool checkmax(int &a, int b) { return (b > a ? a = b, 1 : 0); }
int n, m;
vector<string> mp;
vector<vector<int> > can, can_inv;
vector<int> cnt;
int get(int x, int y) { return x * m + y; }
int one = 0;
void init() {
  cin >> n >> m;
  mp.resize(n);
  cnt.resize(n + m);
  for (int i = 0; i < n; ++i) {
    cin >> mp[i];
  }
  can.resize(n);
  can_inv.resize(n);
  for (int i = 0; i < n; ++i) {
    can[i].resize(m);
    can_inv[i].resize(m);
  }
  int flag = 1;
  for (int i = 0; i < n; ++i) {
    if (mp[i][0] == '#') flag = 0;
    can[i][0] = flag;
  }
  flag = 1;
  for (int i = 0; i < m; ++i) {
    if (mp[0][i] == '#') flag = 0;
    can[0][i] = flag;
  }
  for (int i = 1; i < n; ++i) {
    for (int j = 1; j < m; ++j) {
      if (mp[i][j] == '.' && (can[i - 1][j] || can[i][j - 1])) {
        can[i][j] = 1;
      }
    }
  }
  flag = 1;
  for (int i = n - 1; i >= 0; --i) {
    if (mp[i][m - 1] == '#') flag = 0;
    can_inv[i][m - 1] = flag;
  }
  flag = 1;
  for (int i = m - 1; i >= 0; --i) {
    if (mp[n - 1][i] == '#') flag = 0;
    can_inv[n - 1][i] = flag;
  }
  for (int i = n - 2; i >= 0; --i) {
    for (int j = m - 2; j >= 0; --j) {
      if (mp[i][j] == '.' && (can_inv[i + 1][j] || can_inv[i][j + 1])) {
        can_inv[i][j] = 1;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (can_inv[i][j] && can[i][j]) {
        cnt[i + j]++;
      }
    }
  }
  for (int i = 1; i < n + m - 2; ++i) {
    if (cnt[i] <= 1) {
      one = 1;
    }
  }
}
void solve() {
  if (!can[n - 1][m - 1])
    cout << 0 << endl;
  else if (one)
    cout << 1 << endl;
  else
    cout << 2 << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  init();
  solve();
  return 0;
}
