#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using ll = long long int;
using ld = long double;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(ld x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  char a[n + 2][m + 2];
  int f1[n + 2][m + 2], f2[n + 2][m + 2], d1[n + 2][m + 2], d2[n + 2][m + 2];
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= m + 1; j++) {
      a[i][j] = '#';
      f1[i][j] = f2[i][j] = d1[i][j] = d2[i][j] = 0;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) cin >> a[i][j];
  }
  queue<tuple<int, int, int>> q;
  q.push({1, 1, 0});
  map<pair<int, int>, int> g;
  while (!q.empty()) {
    tuple<int, int, int> m = q.front();
    q.pop();
    int x = get<0>(m), y = get<1>(m);
    int l = get<2>(m);
    d1[x][y] = l;
    g[{x, y}]++;
    if (a[x + 1][y] != '#' and f1[x + 1][y] == 0) {
      q.push({x + 1, y, l + 1});
      f1[x + 1][y] = 1;
    }
    if (a[x][y + 1] != '#' and f1[x][y + 1] == 0) {
      q.push({x, y + 1, l + 1});
      f1[x][y + 1] = 1;
    }
  }
  if (f1[n][m] != 1) {
    cout << 0 << "\n";
    return 0;
  }
  q.push({n, m, 0});
  while (!q.empty()) {
    tuple<int, int, int> m = q.front();
    q.pop();
    int x = get<0>(m), y = get<1>(m);
    int l = get<2>(m);
    d2[x][y] = l;
    g[{x, y}]++;
    if (a[x - 1][y] != '#' and f2[x - 1][y] == 0) {
      q.push({x - 1, y, l + 1});
      f2[x - 1][y] = 1;
    }
    if (a[x][y - 1] != '#' and f2[x][y - 1] == 0) {
      q.push({x, y - 1, l + 1});
      f2[x][y - 1] = 1;
    }
  }
  vector<int> v(n + m + 3, 0);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (g[{i, j}] < 2) {
        continue;
      }
      if (d1[i][j] != 0) {
        v[d1[i][j]]++;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (i == 1 and j == 1) continue;
      if (i == n and j == m) continue;
      if (v[d1[i][j]] == 1) {
        cout << 1 << "\n";
        return 0;
      }
    }
  }
  cout << 2 << "\n";
  return 0;
}
