#include #include #include #include template struct Vector { using V = std::vector; int d; V vec; // constructor Vector(int d, T val = 0) : d(d), vec(d, val) {} // getter T& operator[](int i) { return vec[i]; } T operator[](int i) const { return vec[i]; } typename V::iterator begin() { return vec.begin(); } typename V::iterator end() { return vec.end(); } // arithmetic Vector operator+(const Vector& v) const { return Vector(*this) += v; } Vector operator-(const Vector& v) const { return Vector(*this) -= v; } T operator*(const Vector& v) const { T ret(0); for (int i = 0; i < d; ++i) ret += vec[i] * v[i]; return ret; } // compound assignment Vector& operator+=(const Vector& v) { for (int i = 0; i < d; ++i) vec[i] += v[i]; return *this; } Vector& operator-=(const Vector& v) { for (int i = 0; i < d; ++i) vec[i] -= v[i]; return *this; } }; template struct Matrix { using M = std::vector>; int h, w; M mat; // constructor Matrix(int h, int w, T val = 0) : h(h), w(w), mat(h, std::vector(w, val)) {} static Matrix id(int n) { Matrix m(n, n); for (int i = 0; i < n; ++i) m[i][i] = 1; return m; } // getter std::vector& operator[](int i) { return mat[i]; } std::vector operator[](int i) const { return mat[i]; } typename M::iterator begin() { return mat.begin(); } typename M::iterator end() { return mat.end(); } // arithmetic Matrix operator+(const Matrix& m) const { return Matrix(*this) += m; } Matrix operator-(const Matrix& m) const { return Matrix(*this) -= m; } Matrix operator*(const Matrix& m) const { return Matrix(*this) *= m; } template Matrix pow(U k) { Matrix ret = id(h); Matrix a = *this; while (k > 0) { if (k & 1) ret *= a; a *= a; k >>= 1; } return ret; } // compound assignment Matrix& operator+=(const Matrix& m) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { mat[i][j] += m[i][j]; } } return *this; } Matrix& operator-=(const Matrix& m) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { mat[i][j] -= m[i][j]; } } return *this; } Matrix& operator*=(const Matrix& m) { std::vector> nmat(h, std::vector(m.w, T(0))); for (int i = 0; i < h; ++i) { for (int j = 0; j < m.w; ++j) { for (int k = 0; k < w; ++k) { nmat[i][j] += mat[i][k] * m[k][j]; } } } mat = nmat; return *this; } // arithmetic with vector using Vec = Vector; Vec operator*(const Vec& v) { Vec ret(h, 0); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { ret[i] += mat[i][j] * v[j]; } } return ret; } }; using ldouble = long double; using Vec = Vector; using Mat = Matrix; const std::vector> dxys{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; void solve() { int h, w, t; std::cin >> h >> w >> t; int nn = (h - 2) * (w - 2); auto enc = [&](int x, int y) { return (x - 1) * (w - 2) + (y - 1); }; Vec sv(nn), gv(nn); { int sx, sy, gx, gy; std::cin >> sx >> sy >> gx >> gy; sv[enc(sx, sy)] = 1; gv[enc(gx, gy)] = 1; } std::vector ss(h); for (auto& s : ss) std::cin >> s; Mat m(nn, nn); for (int x = 1; x < h - 1; ++x) { for (int y = 1; y < w - 1; ++y) { if (ss[x][y] == '#') continue; int adj = 0; for (auto [dx, dy] : dxys) { int nx = x + dx, ny = y + dy; if (ss[nx][ny] == '#') continue; ++adj; } if (adj == 0) { m[enc(x, y)][enc(x, y)] = 1; continue; } ldouble p = 1. / adj; for (auto [dx, dy] : dxys) { int nx = x + dx, ny = y + dy; if (ss[nx][ny] == '#') continue; m[enc(nx, ny)][enc(x, y)] = p; } } } m = m.pow(t); std::cout << (m * sv) * gv << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(10); solve(); return 0; }