#include <bits/stdc++.h>
using namespace std;
typedef long long unsigned lulu;
typedef long long signed ll;
int markAndIsVisited(char &tile) {
  int ret = 0;
  if (tile > '.') ret = 1;
  tile += 1;
  return ret;
}
int travel(vector<string> &map, int dSouth, int dEast, int offSouth,
           int offEast) {
  char clear = '.';
  char once = '.' + 1;
  int nowSouth = 0;
  int nowEast = 0;
  int toMoveSouth = 0;
  int toMoveEast = 0;
  int visited = 0;
  dSouth -= 1;
  dEast -= 1;
  stack<pair<int, int> > history;
  pair<int, int> pairTemp(0, 0);
  while (nowSouth <= dSouth && nowEast <= dEast &&
         nowSouth + nowEast != dSouth + dEast) {
    pairTemp.first = nowSouth;
    pairTemp.second = nowEast;
    history.push(pairTemp);
    if (nowSouth + offSouth <= dSouth && nowEast + offEast <= dEast &&
        map[nowSouth + offSouth][nowEast + offEast] != '#') {
      visited = markAndIsVisited(map[nowSouth + offSouth][nowEast + offEast]);
      if (visited && nowSouth + offSouth == dSouth &&
          nowEast + offEast == dEast)
        return 2;
      if (visited) return 1;
      nowSouth += offSouth;
      nowEast += offEast;
      continue;
    }
    bool legalDirChange =
        (nowSouth + offEast <= dSouth && nowEast + offSouth <= dEast);
    if (legalDirChange && map[nowSouth + offEast][nowEast + offSouth] != '#') {
      visited = markAndIsVisited(map[nowSouth + offEast][nowEast + offSouth]);
      if (visited && nowSouth + offEast == dSouth &&
          nowEast + offSouth == dEast)
        return 2;
      if (visited) return 1;
      nowSouth += offEast;
      nowEast += offSouth;
      continue;
    }
    while (true) {
      pair<int, int> lastStep = history.top();
      history.pop();
      if (history.empty()) return 0;
      pairTemp = history.top();
      nowSouth = pairTemp.first;
      nowEast = pairTemp.second;
      if (nowSouth + offEast == lastStep.first &&
          nowEast + offSouth == lastStep.second) {
        map[lastStep.first][lastStep.second] = '#';
        continue;
      }
      map[lastStep.first][lastStep.second] = '#';
      history.pop();
      break;
    }
  }
  return 4;
}
int main() {
  ios::sync_with_stdio(false);
  int dSouth, dEast;
  cin >> dSouth >> dEast;
  vector<string> map(dSouth + 4);
  int i;
  for (i = 0; i < dSouth; i++) {
    cin >> map[i];
  }
  int a, b;
  a = travel(map, dSouth, dEast, 0, 1);
  b = travel(map, dSouth, dEast, 1, 0);
  if (a == 0)
    cout << a;
  else
    cout << b;
  return 0;
}
