#include <bits/stdc++.h>
using namespace std;
const int N = 5000005;
void run() {
  int n, m, i, j;
  pair<int, int> c;
  cin >> n >> m;
  int dp[n][m];
  pair<int, int> par[n][m];
  string s[n];
  memset(dp, 0, sizeof dp);
  for (i = 0; i < n; i++) cin >> s[i];
  dp[0][0] = 1;
  for (j = 1; j < m; j++)
    if (dp[0][j - 1] && s[0][j] == '.')
      dp[0][j] = 1, par[0][j] = make_pair(0, j - 1);
  for (i = 1; i < n; i++)
    if (dp[i - 1][0] && s[i][0] == '.')
      dp[i][0] = 1, par[i][0] = make_pair(i - 1, 0);
  for (i = 1; i < n; i++)
    for (j = 1; j < m; j++) {
      if (s[i][j] == '#') continue;
      if (dp[i - 1][j]) {
        dp[i][j] = 1;
        par[i][j] = make_pair(i - 1, j);
      } else if (dp[i][j - 1]) {
        dp[i][j] = 1;
        par[i][j] = make_pair(i, j - 1);
      }
    }
  if (dp[n - 1][m - 1] == 0) {
    cout << 0;
    return;
  }
  c = make_pair(n - 1, m - 1);
  while (c != make_pair(0, 0)) {
    c = par[c.first][c.second];
    if (c != make_pair(0, 0)) s[c.first][c.second] = '#';
  }
  memset(dp, 0, sizeof dp);
  dp[0][0] = 1;
  for (j = 1; j < m; j++)
    if (dp[0][j - 1] && s[0][j] == '.')
      dp[0][j] = 1, par[0][j] = make_pair(0, j - 1);
  for (i = 1; i < n; i++)
    if (dp[i - 1][0] && s[i][0] == '.')
      dp[i][0] = 1, par[i][0] = make_pair(i - 1, 0);
  for (i = 1; i < n; i++)
    for (j = 1; j < m; j++) {
      if (s[i][j] == '#') continue;
      if (dp[i - 1][j]) {
        dp[i][j] = 1;
        par[i][j] = make_pair(i - 1, j);
      } else if (dp[i][j - 1]) {
        dp[i][j] = 1;
        par[i][j] = make_pair(i, j - 1);
      }
    }
  if (dp[n - 1][m - 1] == 0) {
    cout << 1;
    return;
  }
  cout << 2;
}
void setup() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cout << setprecision(20);
}
signed main() {
  setup();
  int tests = 1;
  for (int i = 1; i <= tests; i++) run();
  return 0;
}
