#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> arr;
int n, m;
vector<vector<bool>> viss;
vector<vector<bool>> vise;
bool bound(int j, int i) {
  return (((j >= 0) && (j < n)) && ((i >= 0) && (i < m)));
}
void DFSs(int j, int i) {
  viss[j][i] = 1;
  if (bound(j + 1, i)) {
    if ((!arr[j + 1][i]) && (!viss[j + 1][i])) {
      DFSs(j + 1, i);
    }
  }
  if (bound(j, i + 1)) {
    if (!(arr[j][i + 1]) && (!viss[j][i + 1])) {
      DFSs(j, i + 1);
    }
  }
}
void DFSe(int j, int i) {
  vise[j][i] = 1;
  if (bound(j - 1, i)) {
    if ((!arr[j - 1][i]) && (!vise[j - 1][i])) {
      DFSe(j - 1, i);
    }
  }
  if (bound(j, i - 1)) {
    if ((!arr[j][i - 1]) && (!vise[j][i - 1])) {
      DFSe(j, i - 1);
    }
  }
}
int solve() {
  cin >> n >> m;
  arr.resize(n, vector<int>(m));
  viss.resize(n, vector<bool>(m, 0));
  vise.resize(n, vector<bool>(m, 0));
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < m; i++) {
      char d;
      cin >> d;
      if (d == '.') {
        arr[j][i] = 0;
      } else {
        arr[j][i] = 1;
      }
    }
  }
  viss[0][0] = 1;
  vise[n - 1][m - 1] = 1;
  DFSs(0, 0);
  DFSe(n - 1, m - 1);
  vector<vector<bool>> dist(n + m + 1);
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < m; i++) {
      dist[j + i].push_back((viss[j][i] && vise[j][i]));
    }
  }
  for (int j = 0; j <= n - 1 + m - 1; j++) {
    int cnt = 0;
    for (int i = 0; i < dist[j].size(); i++) {
      if (dist[j][i]) {
        cnt++;
      }
    }
    if (cnt == 0) {
      cout << 0 << "\n";
      return 0;
    } else if (cnt == 1 && (dist[j].size() > 1)) {
      cout << 1 << "\n";
      return 0;
    }
  }
  if (n == 1 || m == 1) {
    cout << 1 << "\n";
    return 0;
  }
  cout << 2 << "\n";
  return 0;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
