//{{{ #include using namespace std; #define repX(a,b,c,x,...) x #define repN(a) repX a #define rep(...) repN((__VA_ARGS__,rep3,rep2,loop))(__VA_ARGS__) #define rrep(...) repN((__VA_ARGS__,rrep3,rrep2))(__VA_ARGS__) #define loop(n) rep2(i_,n) #define rep2(i,n) rep3(i,0,n) #define rep3(i,begin,end) for(ll i=(ll)(begin),i##_end=(ll)(end);i=i##_end;--i) #define each(x,a) for(auto&x:a) #define sz(x) ((ll)(x).size()) struct IoSetup{ IoSetup(){ cin.tie(nullptr); // ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); }; } ioSetup; using ull=unsigned long long; using ll=long long; using pii=pair; using vp=vector; using vs=vector; using vi=vector; using vvi=vector; #define v(T) vector #define vv(T) v(v(T)) templateostream &operator<<(ostream &o,const pair&j){o<<"{"<ostream &operator<<(ostream &o,const map&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;} templateostream &operator<<(ostream &o,const set&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;} templateostream &operator<<(ostream &o,const multiset&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;} templateostream &operator<<(ostream &o,const vector&j){o<<"{";for(ll i=0;i<(ll)j.size();++i)o<<(i>0?", ":"")<inline void sort(T&a, Compare comp) { sort(a.begin(), a.end(), comp); } template inline void sort(T&a) { sort(a.begin(), a.end()); } template inline void rsort(T&a) { sort(a.rbegin(), a.rend()); } template inline void reverse(T&a) { reverse(a.begin(), a.end()); } template inline bool next_permutation(T&a) { return next_permutation(a.begin(), a.end()); } templateinline bool binary_search(T&a, const U&v) { return binary_search(a.begin(), a.end(), v); } templateinline auto lower_bound(T&a, const U&v) { return lower_bound(a.begin(), a.end(), v); } templateinline auto upper_bound(T&a, const U&v) { return upper_bound(a.begin(), a.end(), v); } template inline T Sum(vector&a){ return accumulate(a.begin(), a.end(), (T)0); } template inline T Max(vector&a){ return *max_element(a.begin(), a.end()); } template inline T Min(vector&a){ return *min_element(a.begin(), a.end()); } template inline bool chmax(T&a, const U&b){ return (b > a) ? (a = b, true) : false; } template inline bool chmin(T&a, const U&b){ return (b < a) ? (a = b, true) : false; } ll gcd(const ll a, const ll b){ return b ? gcd(b, a % b) : a; } ll lcm(const ll a, const ll b){ return a / gcd(a, b) * b; } class in { int n, m; public: in() : n(0),m(0){} in(int n_) : n(n_),m(0){}; in(int n_, int m_) : n(n_),m(m_){}; template operator T() {assert(n==0); assert(m==0); T ret; cin >> ret; return ret; } template operator vector() {assert(n>0); assert(m==0); vector ret(n); for(ll i=0;i<(ll)n;++i) cin>>ret[i]; return ret; } template operator vector>() {assert(n>0); assert(m>0); vector> ret(n, vector(m)); for(ll i=0;i<(ll)n;++i) for(ll j=0;j<(ll)m;++j) cin>>ret[i][j]; return ret; } }; template void print(const T& a){ cout << a; } inline int out(){ cout << '\n'; return 0; } template inline int out(const T& t){ print(t); cout<<'\n'; return 0; } template inline int out(const Head& head, const Tail&... tail){ print(head); cout << " "; out(tail...); return 0; } #ifdef LOCAL #include "console_color.hpp" template void debug_print(const T& a){ cerr << a; } inline void dump(){ setColor(); cerr << " \n"; } template inline void dump(const T& t){ debug_print(t); setColor(); cerr << " \n"; } template inline void dump(const Head& head, const Tail&... tail){ setColor(COL_WHITE, COL_DARK_YELLOW); debug_print(head); cerr<<" "; dump(tail...); } #define debug(...) do{ setColor(COL_WHITE, COL_DARK_BLUE); cerr<<"[L."<<__LINE__<<"] "<<#__VA_ARGS__<<": "; dump(__VA_ARGS__); }while(0) #else #define dump(...) #define debug(...) #endif template vector make_vector(size_t a){return vector(a);} template auto make_vector(size_t a, Tail... tail){ return vector(tail...))>(a, make_vector(tail...)); } #define Vector make_vector template class Modular { using u64 = std::uint_fast64_t; public: u64 a; constexpr Modular(std::int_fast64_t x = 0) noexcept : a(x % Mod + (x < 0 ? Mod : 0)) {} constexpr u64 val() const noexcept { return a; } constexpr Modular operator+(const Modular rhs) const noexcept { return Modular(*this) += rhs; } constexpr Modular operator-(const Modular rhs) const noexcept { return Modular(*this) -= rhs; } constexpr Modular operator*(const Modular rhs) const noexcept { return Modular(*this) *= rhs; } constexpr Modular operator/(const Modular rhs) const noexcept { return Modular(*this) /= rhs; } constexpr bool operator==(const Modular rhs) const noexcept { return Modular(*this).val() == rhs.val(); } Modular &operator+=(const Modular rhs) noexcept { a += rhs.a; if (a >= Mod) a -= Mod; return *this; } Modular &operator-=(const Modular rhs) noexcept { if (a < rhs.a) a += Mod; a -= rhs.a; return *this; } Modular &operator++() noexcept { return *this += 1; } Modular &operator--() noexcept { return *this -= 1; } Modular &operator*=(const Modular rhs) noexcept { a = a * rhs.a % Mod; return *this; } Modular &operator/=(Modular rhs) noexcept { u64 exp = Mod - 2; while(exp){ if(exp % 2) *this *= rhs; rhs *= rhs; exp /= 2; } return *this; } }; template ostream& operator<<(ostream& os, const Modular& m){ return os << m.a; } const double pi=acos(-1); const double eps = 1e-9; const ll inf = 1001001001; const ll mod=(ll)1e9+7; using mint = Modular; //}}} int main(){ ll H = in(); ll W = in(); vs m = in(H); vvi mp = Vector(H, W); rep(h, H){ rep(w, W){ mp[h][w] = -1; } } priority_queue q; q.emplace(0, 0); mp[0][0] = 1; while(q.size()){ ll v = -q.top().first; ll x0 = q.top().second % W; ll y0 = q.top().second / W; q.pop(); ll dx[] = {0, 1}; ll dy[] = {1, 0}; rep(i, 2){ ll x = x0 + dx[i]; ll y = y0 + dy[i]; if(x == -1) continue; if(y == -1) continue; if(x == W) continue; if(y == H) continue; if(mp[y][x] != -1) continue; mp[y][x] = v + 1 + ((m[y][x] == 'k') ? x + y : 0); q.emplace(-mp[y][x], y * W + x); } } ll ans = mp[H - 1][W - 1]; out(ans); }