#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 7;
const int INF = 1e9 + 7;
int dis[N];
int n, m;
int main() {
  cin >> n >> m;
  int ans = 2;
  vector<char> a[n + 5];
  vector<int> us[n + 5], pr[n + 5];
  for (int i = 1; i <= n; i++) {
    a[i].resize(m + 5);
    us[i].resize(m + 5);
    pr[i].resize(m + 5);
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }
  queue<pair<int, pair<int, int> > > q;
  q.push(make_pair(0, make_pair(1, 1)));
  while (!q.empty()) {
    int d = q.front().first, x = q.front().second.first,
        y = q.front().second.second;
    q.pop();
    if (us[x][y]) continue;
    us[x][y] = 1;
    if (x + 1 <= n && a[x + 1][y] == '.') {
      q.push(make_pair(d + 1, make_pair(x + 1, y)));
    }
    if (y + 1 <= m && a[x][y + 1] == '.') {
      q.push(make_pair(d + 1, make_pair(x, y + 1)));
    }
  }
  q.push(make_pair(n + m - 2, make_pair(n, m)));
  while (!q.empty()) {
    int d = q.front().first, x = q.front().second.first,
        y = q.front().second.second;
    q.pop();
    if (us[x][y] == 2) continue;
    us[x][y] = 2;
    dis[d]++;
    if (x - 1 >= 1 && a[x - 1][y] == '.' && us[x - 1][y] == 1) {
      q.push(make_pair(d - 1, make_pair(x - 1, y)));
    }
    if (y - 1 >= 1 && a[x][y - 1] == '.' && us[x][y - 1] == 1) {
      q.push(make_pair(d - 1, make_pair(x, y - 1)));
    }
  }
  int mn = 2;
  for (int i = 1; i <= n + m - 3; i++) {
    mn = min(mn, dis[i]);
  }
  cout << mn << endl;
}
