#include <bits/stdc++.h>
using namespace std;
int n, m;
string s[1000001];
int d[3000001];
bool ok = false;
bool dfs(int x, int y) {
  bool t1 = false;
  bool t2 = false;
  if (x == 3 && y == 4) {
    int x = 1;
  }
  if (x > n || y > m || x < 0 || y < 0 || s[x][y] == '#' || d[x * m + y])
    return false;
  if (x == n && y == m) return true;
  t1 = dfs(x + 1, y);
  if (t1) {
    s[x][y] = '#';
    return true;
  }
  t2 = dfs(x, y + 1);
  if ((t1 || t2)) {
    s[x][y] = '#';
  }
  d[x * m + y] = true;
  return t1 || t2;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    s[i] = '.' + s[i];
  }
  if (!dfs(1, 1)) {
    cout << 0;
    return 0;
  }
  s[n][m] = '.';
  d[n * m + m] = false;
  memset(d, false, sizeof d);
  s[1][1] = '.';
  if (!dfs(1, 1)) {
    cout << 1;
    return 0;
  }
  cout << 2;
  return 0;
}
