#include using namespace std; #define rep(i, n) for(int i = 0; i < (int)n; ++i) #define FOR(i, a, b) for(int i = a; i < (int)b; ++i) #define rrep(i, n) for(int i = ((int)n - 1); i >= 0; --i) using ll = long long; using ld = long double; const int Inf = 1e9; const double EPS = 1e-9; const int MOD = 1e9 + 7; int main() { cin.tie(nullptr); ios::sync_with_stdio(0); int h, w; cin >> h >> w; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; vector s(h); vector > dist(h, vector(w, Inf)); rep (i, h) cin >> s[i]; queue > q; q.push({0, 0}); dist[0][0] = 0; while (!q.empty()) { int x, y; x = q.front().first, y = q.front().second; q.pop(); rep (i, 4) { int nx, ny; nx = x + dx[i]; ny = y + dy[i]; if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue; if (dist[nx][ny] < dist[x][y] + 1) continue; if (s[nx][ny] == '.') { dist[nx][ny] = dist[x][y] + 1; q.push({nx, ny}); continue; } dist[nx][ny] = dist[x][y] + nx + ny + 1; q.push({nx, ny}); } } cout << dist[h - 1][w - 1] << endl; return 0; }