#include #define REP(i, n) for (int i = 0; (i) < int(n); ++ (i)) using namespace std; template auto vectors(X x, T a) { return vector(x, a); } template auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector(x, cont); } const int dy[] = { -1, 1, 0, 0 }; const int dx[] = { 0, 0, 1, -1 }; vector > operator * (vector > const & a, vector > const & b) { int n = a.size(); vector > c = vectors(n, n, double()); REP (y, n) REP (z, n) REP (x, n) c[y][x] = c[y][x] + a[y][z] * b[z][x]; return c; } vector operator * (vector > const & a, vector const & b) { int n = a.size(); vector c(n); REP (y, n) REP (z, n) c[y] = c[y] + a[y][z] * b[z]; return c; } vector > unit_matrix(int n) { vector > e = vectors(n, n, double()); REP (i, n) e[i][i] = 1; return e; } vector > zero_matrix(int n) { vector > o = vectors(n, n, double()); return o; } vector > powmat(vector > x, long long y) { int n = x.size(); auto z = unit_matrix(n); for (long long i = 1; i <= y; i <<= 1) { if (y & i) z = z * x; x = x * x; } return z; } int main() { // input int h, w, t; scanf("%d%d%d", &h, &w, &t); int sy, sx; scanf("%d%d", &sy, &sx); int gy, gx; scanf("%d%d", &gy, &gx); auto f = vectors(h, w, char()); REP (y, h) REP (x, w) scanf(" %c", &f[y][x]); // solve vector > a = zero_matrix(h * w); REP (y, h) REP (x, w) { if (f[y][x] == '#') continue; int cnt = 0; REP (dir, 4) { int ny = y + dy[dir]; int nx = x + dx[dir]; if (f[ny][nx] == '#') continue; cnt += 1; } if (cnt == 0) { a[y * w + x][y * w + x] += 1.0; } else { REP (dir, 4) { int ny = y + dy[dir]; int nx = x + dx[dir]; if (f[ny][nx] == '#') continue; a[ny * w + nx][y * w + x] += 1.0 / cnt; } } } vector b(h * w); b[sy * w + sx] = 1.0; double result = (powmat(a, t) * b)[gy * w + gx]; // output printf("%.16lf\n", result); return 0; }