#include <bits/stdc++.h>
using namespace std;
const int LIM = 1e6 + 239;
int n, m;
vector<string> gr;
int get_num(int i, int j) { return i * m + j; }
int ok(int i, int j) {
  return i >= 0 && i < n && j >= 0 && j < m && gr[i][j] == '.';
}
vector<int> get_next(int v, int to) {
  int i = v / m;
  int j = v % m;
  vector<int> ans;
  if (to) {
    if (ok(i + 1, j)) {
      ans.push_back(get_num(i + 1, j));
    }
    if (ok(i, j + 1)) {
      ans.push_back(get_num(i, j + 1));
    }
  } else {
    if (ok(i - 1, j)) {
      ans.push_back(get_num(i - 1, j));
    }
    if (ok(i, j - 1)) {
      ans.push_back(get_num(i, j - 1));
    }
  }
  return ans;
}
void bfs(int st, int to, vector<int> &d) {
  d.resize(n * m, LIM);
  d[st] = 0;
  queue<int> q;
  q.push(st);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    auto nx = get_next(v, to);
    for (auto t : nx) {
      if (d[t] > d[v] + 1) {
        d[t] = d[v] + 1;
        q.push(t);
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  gr.resize(n);
  for (auto &t : gr) {
    cin >> t;
  }
  vector<int> fd, sd;
  bfs(get_num(0, 0), 1, fd);
  bfs(get_num(n - 1, m - 1), 0, sd);
  if (fd[get_num(n - 1, m - 1)] == LIM) {
    cout << 0 << endl;
    exit(0);
  }
  vector<int> hv(LIM);
  for (int i = 0; i < n * m; i++) {
    if (i == get_num(0, 0) || i == get_num(n - 1, m - 1)) {
      continue;
    }
    if (fd[i] + sd[i] == fd[get_num(n - 1, m - 1)]) {
      hv[fd[i]]++;
    }
  }
  for (int i = 0; i < LIM; i++) {
    if (hv[i] == 1) {
      cout << 1 << endl;
      exit(0);
    }
  }
  cout << 2 << endl;
}
