#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adj, rev;
int main() {
  cout.sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<string> vs;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    vs.push_back(s);
  }
  adj.resize(n * m + 5);
  rev.resize(n * m + 5);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (vs[i][j] != '.') continue;
      int u = i * m + j;
      int v1 = i * m + j + m;
      int v2 = i * m + j + 1;
      if (i + 1 < n && vs[i + 1][j] == '.')
        adj[u].push_back(v1), rev[v1].push_back(u);
      if (j + 1 < m && vs[i][j + 1] == '.')
        adj[u].push_back(v2), rev[v2].push_back(u);
    }
  }
  auto solve = [&](int src, int flag) {
    queue<int> q;
    q.push(src);
    vector<int> dep(n * m + 5, -1);
    dep[src] = 0;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      if (flag == 0 && u == n * m - 1) continue;
      if (flag == 1 && u == 0) continue;
      auto& vec = (flag == 0 ? adj[u] : rev[u]);
      for (int v : vec)
        if (dep[v] == -1) {
          dep[v] = dep[u] + 1;
          q.push(v);
        }
    }
    return dep;
  };
  vector<int> d1 = solve(0, 0);
  vector<int> d2 = solve(n * m - 1, 1);
  if (d1[n * m - 1] == -1)
    cout << 0 << endl;
  else {
    vector<int> cnt(n * m + 5, 0);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        int u = i * m + j;
        if (d1[u] != -1 && d2[u] != -1) cnt[d1[u]]++;
      }
    for (int i = 1; i < n * m - 1; i++)
      if (d1[i] != -1 && cnt[d1[i]] == 1) {
        cout << 1 << endl;
        return 0;
      }
    cout << 2 << endl;
  }
  return 0;
}
