#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<char> > mtr(n, vector<char>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) cin >> mtr[i][j];
  }
  vector<vector<int> > visited(n, vector<int>(m));
  vector<vector<int> > dist(n, vector<int>(m));
  queue<pair<int, int> > q;
  q.push(make_pair(0, 0));
  visited[0][0] = 1;
  while (!q.empty()) {
    auto var = q.front();
    q.pop();
    int x = var.first, y = var.second;
    if (x + 1 < n && !visited[x + 1][y] && mtr[x + 1][y] == '.') {
      visited[x + 1][y] = 1;
      dist[x + 1][y] = 1 + dist[x][y];
      q.push(make_pair(x + 1, y));
    }
    if (y + 1 < m && !visited[x][y + 1] && mtr[x][y + 1] == '.') {
      visited[x][y + 1] = 1;
      dist[x][y + 1] = 1 + dist[x][y];
      q.push(make_pair(x, y + 1));
    }
  }
  if (!visited[n - 1][m - 1]) {
    cout << "0";
    return 0;
  }
  visited[n - 1][m - 1] = 2;
  q.push(make_pair(n - 1, m - 1));
  while (!q.empty()) {
    auto var = q.front();
    q.pop();
    int x = var.first, y = var.second;
    if (x - 1 >= 0 && visited[x - 1][y] == 1 && mtr[x - 1][y] == '.') {
      visited[x - 1][y] = 2;
      q.push(make_pair(x - 1, y));
    }
    if (y - 1 >= 0 && visited[x][y - 1] == 1 && mtr[x][y - 1] == '.') {
      visited[x][y - 1] = 2;
      q.push(make_pair(x, y - 1));
    }
  }
  map<int, int> rel;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (visited[i][j] == 2) rel[dist[i][j]]++;
    }
  }
  int ans = 2;
  for (auto var : rel) {
    if (var.first != 0 && var.first != (n + m - 2)) ans = min(ans, var.second);
  }
  cout << ans;
  return 0;
}
