#include <bits/stdc++.h>
using namespace std;
using ll = long long;
inline int read();
const int M = 1000016, MOD = 1000000007;
const int go[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
int n, m;
string mp[M];
vector<bool> vis_by_src[M];
vector<bool> vis_by_dst[M];
int dis_cnt[M];
void bfs(int sx, int sy, vector<bool> *vis, int ck) {
  queue<pair<int, int>> q;
  q.push({sx, sy});
  vis[sx][sy] = 1;
  while (!q.empty()) {
    int ux = q.front().first, uy = q.front().second;
    q.pop();
    for (int k = ck; k <= ck + 1; ++k) {
      int vx = ux + go[k][0], vy = uy + go[k][1];
      if (vx < 0 || vy < 0 || vx >= n || vy >= m || mp[vx][vy] == '#' ||
          vis[vx][vy])
        continue;
      vis[vx][vy] = 1;
      q.push({vx, vy});
    }
  }
}
int main(void) {
  n = read(), m = read();
  for (int i = 0; i < n; ++i) {
    cin >> mp[i];
    vis_by_src[i] = vector<bool>(m, 0);
    vis_by_dst[i] = vector<bool>(m, 0);
  }
  bfs(0, 0, vis_by_src, 0);
  bfs(n - 1, m - 1, vis_by_dst, 2);
  if (vis_by_src[n - 1][m - 1] == 0) {
    printf("0\n");
  } else {
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) {
        int d = i + j;
        if (d == 0 || d == n + m - 2 || mp[i][j] == '#') continue;
        if (vis_by_src[i][j] && vis_by_dst[i][j]) {
          dis_cnt[d]++;
        }
      }
    for (int dis = 1; dis < n + m - 2; ++dis) {
      if (dis_cnt[dis] == 1) {
        printf("1\n");
        return 0;
      }
    }
    printf("2\n");
  }
  return 0;
}
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
