#include <bits/stdc++.h>
int num[1000010];
int step[1000010];
int mmap[1000010];
int _map[1000010];
int que[1000010][2];
int n, m;
int suan(int x, int y) {
  if (x > n || y > m || x < 1 || y < 1) return 0;
  return (x - 1) * m + y;
}
void fin(int x, int y) {
  mmap[suan(x, y)] = 1;
  if (_map[suan(x - 1, y)] && !mmap[suan(x - 1, y)]) fin(x - 1, y);
  if (_map[suan(x, y - 1)] && !mmap[suan(x, y - 1)]) fin(x, y - 1);
}
int main() {
  scanf("%d%d", &n, &m);
  int mmstep = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      step[suan(i, j)] = 0;
      char ch = getchar();
      while (ch != '.' && ch != '#') ch = getchar();
      if (ch == '.')
        _map[suan(i, j)] = 1;
      else
        _map[suan(i, j)] = 0;
    }
  fin(n, m);
  if (!mmap[suan(1, 1)]) {
    printf("0\n");
    return 0;
  }
  int tt = 1, ll = 0;
  que[ll][0] = 1;
  que[ll][1] = 1;
  while (ll < tt) {
    int x = que[ll][0], y = que[ll][1];
    int nowtp = step[suan(x, y)];
    num[nowtp]++;
    if (nowtp > mmstep) mmstep = nowtp;
    int tmp = suan(x + 1, y);
    if (x < n && mmap[tmp] && (!step[tmp])) {
      que[tt][0] = x + 1;
      que[tt][1] = y;
      step[suan(x + 1, y)] = nowtp + 1;
      tt++;
    }
    tmp = suan(x, y + 1);
    if (y < m && mmap[tmp] && !step[tmp]) {
      que[tt][0] = x;
      que[tt][1] = y + 1;
      step[suan(x, y + 1)] = nowtp + 1;
      tt++;
    }
    ll++;
  }
  int ans = 2;
  if (mmstep != n + m - 2) ans = 0;
  for (int i = 1; i < mmstep; i++)
    if (num[i] < ans) ans = num[i];
  printf("%d\n", ans);
}
