#include using namespace std; const int N = 20; const int dy[] = { -1, 0, 1, 0 }; const int dx[] = { 0, 1, 0, -1 }; char world[N][N+1]; bool is_tunnel[N][N]; pair tunnels[N][N]; double prob[2][N][N]; int main() { int n, m, k; cin >> n >> m >> k; for (int i = 0; i < n; i++) cin >> world[i]; for (int i = 0; i < k; i++) { int a, b, c, d; cin >> a >> b >> c >> d; a--; b--; c--; d--; tunnels[a][b] = make_pair(c, d); tunnels[c][d] = make_pair(a, b); is_tunnel[a][b] = is_tunnel[c][d] = true; } int ai, aj; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { switch (world[i][j]) { case '%': prob[0][i][j] = prob[1][i][j] = 1; break; case '#': case '*': prob[0][i][j] = prob[1][i][j] = 0; break; case 'A': ai = i, aj = j; // fallthru default: prob[0][i][j] = prob[1][i][j] = 0; break; } } } int curr = 0, next = 1; for (int iter = 0; iter < 100000; iter++) { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (world[i][j] == '#' || world[i][j] == '*' || world[i][j] == '%') continue; int neighbors = 0; prob[next][i][j] = 0; for (int d = 0; d < 4; d++) { int ii = i + dy[d], jj = j + dx[d]; if (ii < 0 || ii >= n || jj < 0 || jj >= m || world[ii][jj] == '#') continue; neighbors++; if (is_tunnel[ii][jj]) { pair dest = tunnels[ii][jj]; ii = dest.first, jj = dest.second; } prob[next][i][j] += prob[curr][ii][jj]; } if (neighbors > 0) prob[next][i][j] /= neighbors; } curr = next; next = 1-curr; } printf("%.7f\n", prob[curr][ai][aj]); return 0; }