#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w;
  cin >> h >> w;
  vector<vector<int>> in(h, vector<int>(w)), from_st(in), from_g(in), vis(in);
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      in[i][j] = s[j] == '#';
    }
  }
  queue<pair<int, int>> que;
  que.push({0, 0});
  from_st[0][0] = 1;
  while (que.size()) {
    auto cur = que.front();
    que.pop();
    int x = cur.first + 1;
    int y = cur.second;
    if (x >= h || y >= w || in[x][y])
      ;
    else if (!from_st[x][y])
      que.push({x, y}), from_st[x][y] = 1;
    x = cur.first;
    y = cur.second + 1;
    if (x >= h || y >= w || in[x][y])
      ;
    else if (!from_st[x][y])
      que.push({x, y}), from_st[x][y] = 1;
  }
  que.push({h - 1, w - 1});
  from_g[h - 1][w - 1] = 1;
  while (que.size()) {
    auto cur = que.front();
    que.pop();
    int x = cur.first - 1;
    int y = cur.second;
    if (x < 0 || y < 0 || in[x][y])
      ;
    else if (!from_g[x][y])
      que.push({x, y}), from_g[x][y] = 1;
    x = cur.first;
    y = cur.second - 1;
    if (x < 0 || y < 0 || in[x][y])
      ;
    else if (!from_g[x][y])
      que.push({x, y}), from_g[x][y] = 1;
  }
  que.push({0, 0});
  vis[0][0] = 1;
  int ans = 1 << 30;
  from_st[0][0] = from_g[0][0] = from_st[h - 1][w - 1] = from_g[h - 1][w - 1] =
      1;
  while (que.size()) {
    auto cur = que.front();
    if (cur != make_pair(0, 0) && cur != make_pair(h - 1, w - 1)) {
      ans = min(ans, (int)que.size());
    }
    que.pop();
    int x = cur.first + 1;
    int y = cur.second;
    if (x >= h || y >= w || (!from_st[x][y] || !from_g[x][y]) || in[x][y])
      ;
    else if (!vis[x][y])
      que.push({x, y}), vis[x][y] = 1;
    x = cur.first;
    y = cur.second + 1;
    if (x >= h || y >= w || (!from_st[x][y] || !from_g[x][y]) || in[x][y])
      ;
    else if (!vis[x][y])
      que.push({x, y}), vis[x][y] = 1;
  }
  cout << (vis[h - 1][w - 1] ? ans : 0) << endl;
  return 0;
}
