#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n, m;
char str[N];
bool dr[N][4], dp[N], A[N];
bool chk(int id) {
  int c = 0;
  for (int i = 0, i_ = 3; i <= i_; i++) c += dr[id][i];
  return c >= 2;
}
int main() {
  cin >> n >> m;
  dp[1] = 1;
  for (int i = 1, i_ = n; i <= i_; i++) {
    scanf("%s", str + (i - 1) * m + 1);
    for (int j = 1, j_ = m; j <= j_; j++)
      if (i != 1 || j != 1) {
        int id = (i - 1) * m + j;
        if (str[id] == '#') continue;
        if (i == 1)
          dp[id] = dp[id - 1];
        else if (j == 1)
          dp[id] = dp[id - m];
        else
          dp[id] = (dp[id - 1] | dp[id - m]);
      }
  }
  A[n * m] = 1;
  for (int i = n, i_ = 1; i >= i_; i--)
    for (int j = m, j_ = 1; j >= j_; j--)
      if (i != n || j != m) {
        int id = (i - 1) * m + j;
        if (str[id] == '#') continue;
        if (i == n)
          A[id] = A[id + 1];
        else if (j == m)
          A[id] = A[id + m];
        else
          A[id] = (A[id + 1] | A[id + m]);
      }
  for (int i = 1, i_ = n * m; i <= i_; i++) A[i] &= dp[i];
  int ans = 2;
  for (int i = 2, i_ = n + m - 1; i <= i_; i++) {
    int x = min(i, n), y = max(1, i - n);
    int cnt = 0;
    while (x && y <= m) cnt += A[(x - 1) * m + y], x--, y++;
    ans = min(ans, cnt);
  }
  printf("%d\n", ans);
  return 0;
}
