#include using namespace std; using ll = long long int; using P = pair; using P3 = pair; using PP = pair; constexpr int INF = 1<<30; constexpr ll MOD = (1e9)+7; constexpr int di[] = {0,1,0,-1}; constexpr int dj[] = {1,0,-1,0}; int main(){ int h, w; cin >> h >> w; vector s(h); for(int i=0;i> s[i]; } vector > d(h, vector(w, INF)); d[0][0] = 0; priority_queue,greater > que; que.push(make_pair(0LL,P(0,0))); while(!que.empty()){ auto p = que.top(); que.pop(); int i = p.second.first, j = p.second.second; if(d[i][j] < p.first) continue; for(int k=0;k<2;k++){ int ni = i+di[k], nj=j+dj[k]; if(ni<0||ni>=h)continue; if(nj<0||nj>=w)continue; ll nd; if(s[ni][nj] == '.'){ nd = d[i][j] + 1; }else{ nd = d[i][j] + 1 + ni + nj; } if(d[ni][nj] > nd){ d[ni][nj] = nd; que.push(make_pair(d[ni][nj],P(ni,nj))); } } } cout << d[h-1][w-1] << endl; return 0; }