#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<char>> forest;
vector<vector<bool>> visited_from_start;
vector<vector<bool>> visited_from_end;
vector<int> intersects;
bool check(int i, int j) {
  return i >= 1 && i <= n && j >= 1 && j <= m && forest[i][j] == '.';
}
void dfsFromStart(int i, int j) {
  visited_from_start[i][j] = true;
  if (check(i + 1, j) && !visited_from_start[i + 1][j]) dfsFromStart(i + 1, j);
  if (check(i, j + 1) && !visited_from_start[i][j + 1]) dfsFromStart(i, j + 1);
}
void dfsFromEnd(int i, int j) {
  visited_from_end[i][j] = true;
  if (check(i - 1, j) && !visited_from_end[i - 1][j]) dfsFromEnd(i - 1, j);
  if (check(i, j - 1) && !visited_from_end[i][j - 1]) dfsFromEnd(i, j - 1);
}
void printForest() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cout << forest[i][j];
    }
    cout << endl;
  }
}
void printSolution() {
  cout << "==============================" << endl;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cout << visited_from_start[i][j];
    }
    cout << endl;
  }
  cout << "==============================" << endl;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cout << visited_from_end[i][j];
    }
    cout << endl;
  }
}
void solution() {
  for (int i = 1; i < n * m; i++) {
    if (intersects[i] == 1) {
      cout << 1 << endl;
      return;
    }
  }
  for (int i = 1; i < n * m; i++) {
    if (intersects[i] == 2) {
      cout << 2 << endl;
      return;
    }
  }
  cout << 0 << endl;
  return;
}
void solve() {
  intersects = vector<int>(n * m);
  dfsFromStart(1, 1);
  dfsFromEnd(n, m);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (i == 1 && j == 1) continue;
      if (i == n && j == m) continue;
      if (visited_from_start[i][j] && visited_from_end[i][j]) {
        intersects[i + j - 2]++;
      }
    }
  }
  solution();
}
void input() {
  char temp;
  cin >> n >> m;
  forest = vector<vector<char>>(n + 1);
  visited_from_start = vector<vector<bool>>(n + 1);
  visited_from_end = vector<vector<bool>>(n + 1);
  for (int i = 0; i <= n; i++) {
    forest[i] = vector<char>(m + 1);
    visited_from_start[i] = vector<bool>(m + 1);
    visited_from_end[i] = vector<bool>(m + 1);
    for (int j = 0; j <= m; j++) {
      if (i > 0 && j > 0) {
        cin >> forest[i][j];
      }
    }
  }
}
int main() {
  input();
  solve();
}
