#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b) {
  long long res = 1;
  a = a % 998244353;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % 998244353;
      b--;
    }
    a = (a * a) % 998244353;
    b >>= 1;
  }
  return res;
}
long long fermat_inv(long long y) { return power(y, 998244353 - 2); }
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  while (T--) {
    int n, m;
    cin >> n >> m;
    string s[n];
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }
    int ans = 2;
    vector<int> adj[n * m];
    vector<int> adj1[n * m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (i + 1 < n && s[i + 1][j] != '#') {
          adj[i * m + j].push_back((i + 1) * m + j);
          adj1[(i + 1) * m + j].push_back(i * m + j);
        }
        if (j + 1 < m && s[i][j + 1] != '#') {
          adj[i * m + j].push_back(i * m + j + 1);
          adj1[i * m + j + 1].push_back(i * m + j);
        }
      }
    }
    queue<int> q;
    vector<bool> used(n * m, false);
    vector<int> d(n * m, 0);
    q.push(0);
    used[0] = true;
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int u : adj[v]) {
        if (!used[u]) {
          used[u] = true;
          q.push(u);
          d[u] = d[v] + 1;
        }
      }
    }
    vector<bool> used1(n * m, false);
    q.push(n * m - 1);
    used1[n * m - 1] = true;
    vector<int> d1(n * m, 0);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int u : adj1[v]) {
        if (!used1[u]) {
          used1[u] = true;
          q.push(u);
          d1[u] = d1[v] + 1;
        }
      }
    }
    if (used[n * m - 1] == false) {
      ans = 0;
      cout << "0\n";
      continue;
    }
    vector<int> ctr(n + m - 1, 0);
    for (int i = 0; i < n * m; i++) {
      if (used[i] && used1[i] && d[i] + d1[i] == n + m - 2)
        ctr[i / m + i % m]++;
    }
    for (int i = 1; i < n + m - 2; i++) {
      ans = min(ctr[i], ans);
    }
    cout << ans << "\n";
  }
}
