#include <bits/stdc++.h>
using namespace std;
vector<string> grid;
vector<vector<int>> mark;
vector<vector<int>> mark2;
int cnt[2000010];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  grid.resize(n);
  mark.resize(n);
  mark2.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> grid[i];
    mark[i].resize(m);
    mark2[i].resize(m);
    for (int j = 0; j < m; j++) {
      if (grid[i][j] == '#')
        mark[i][j] = -1;
      else
        mark[i][j] = 0;
      mark2[i][j] = mark[i][j];
    }
  }
  mark[0][0] = 1;
  queue<int> qx, qy;
  qx.push(0);
  qy.push(0);
  while (!qx.empty()) {
    int cx = qx.front();
    qx.pop();
    int cy = qy.front();
    qy.pop();
    int nx, ny;
    nx = cx + 1, ny = cy;
    if (nx < n && mark[nx][ny] == 0) {
      mark[nx][ny] = 1;
      qx.push(nx);
      qy.push(ny);
    }
    nx = cx, ny = cy + 1;
    if (ny < m && mark[nx][ny] == 0) {
      mark[nx][ny] = 1;
      qx.push(nx);
      qy.push(ny);
    }
  }
  if (mark[n - 1][m - 1] != 1) {
    return 0 * puts("0");
  }
  mark2[n - 1][m - 1] = 1;
  assert(qx.empty());
  assert(qy.empty());
  qx.push(n - 1);
  qy.push(m - 1);
  while (!qx.empty()) {
    int cx = qx.front();
    qx.pop();
    int cy = qy.front();
    qy.pop();
    int nx, ny;
    nx = cx - 1, ny = cy;
    if (nx >= 0 && mark2[nx][ny] == 0) {
      mark2[nx][ny] = 1;
      qx.push(nx);
      qy.push(ny);
    }
    nx = cx, ny = cy - 1;
    if (ny >= 0 && mark2[nx][ny] == 0) {
      mark2[nx][ny] = 1;
      qx.push(nx);
      qy.push(ny);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (mark[i][j] == 1 && mark2[i][j] == 1) {
        cnt[i + j] += 1;
      }
    }
  }
  int ans = 2;
  for (int i = 1; i < n + m - 2; i++) {
    ans = min(ans, cnt[i]);
  }
  printf("%d\n", ans);
  return 0;
}
