#include <bits/stdc++.h>
using namespace std;
int n, m;
int dir[2];
struct node {
  int x;
  int dep;
};
char maze[1000010];
int badmap[1000010];
int vis[1000010];
int mdep[1000010];
char t[1000010];
queue<node> q;
int flag;
int bfs() {
  while (!q.empty()) {
    node now = q.front();
    q.pop();
    if (now.x == n * m - 1) flag = 1;
    for (int i = 0; i < 2; ++i) {
      int nx = now.x + dir[i];
      int ndep = now.dep + 1;
      if (nx >= n * m) continue;
      if (i == 0 && (nx / m) != (now.x / m)) continue;
      if (vis[nx] != 1 && maze[nx] != '#') {
        vis[nx] = 1;
        node nex;
        nex.x = nx;
        nex.dep = ndep;
        q.push(nex);
      }
    }
  }
  if (flag) return 1;
  return -1;
}
void bfs2() {
  while (!q.empty()) {
    node now = q.front();
    q.pop();
    for (int i = 0; i < 2; ++i) {
      int nx = now.x - dir[i];
      int ndep = now.dep + 1;
      if (nx < 0) continue;
      if (i == 0 && (nx / m) != (now.x / m)) continue;
      if (vis[nx] == 1 && maze[nx] != '#') {
        ++mdep[ndep];
        ++vis[nx];
        node nex;
        nex.x = nx;
        nex.dep = ndep;
        q.push(nex);
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  dir[0] = 1;
  dir[1] = m;
  int tot = -1;
  for (int i = 1; i <= n; ++i) {
    scanf("%s", t + 1);
    for (int j = 1; j <= m; ++j) maze[++tot] = t[j];
  }
  node start;
  start.x = 0;
  start.dep = 0;
  vis[0] = 1;
  q.push(start);
  if (bfs() == -1) {
    cout << 0 << endl;
    return 0;
  }
  while (!q.empty()) q.pop();
  start.x = n * m - 1;
  start.dep = 1;
  q.push(start);
  bfs2();
  for (int i = 2; i < n + m - 1; ++i) {
    if (mdep[i] == 1) {
      cout << 1 << endl;
      return 0;
    }
  }
  cout << 2 << endl;
  return 0;
}
