#include #include using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() #define ll long long #define ld long double #define INF 1000000000000000000 typedef pair pll; typedef pair pint; int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector> G(H, vector(W)); rep(i, H) { rep(j, W) { cin >> G[i][j]; } } vector> dp(H, vector(W, INF)); int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; dp[0][0] = 0; rep(i, H) { rep(j, W) { if (i == 0 && j == 0) continue; if (G[i][j] == '.') { ll tmp1 = (i - 1 >= 0) ? dp[i - 1][j] : INF; ll tmp2 = (j - 1 >= 0) ? dp[i][j - 1] : INF; dp[i][j] = min(tmp1, tmp2) + 1; } else { ll tmp1 = (i - 1 >= 0) ? dp[i - 1][j] : INF; ll tmp2 = (j - 1 >= 0) ? dp[i][j - 1] : INF; dp[i][j] = min(tmp1, tmp2) + 1 + i + j; } } } cout << dp[H - 1][W - 1] << endl; }