#include using namespace std; double DP[21][21]; char g[21][21]; map< pair, pair > m; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, 1, 0, -1}; int main() { int N, M; cin >> N >> M; int t = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { cin >> g[i][j]; if (g[i][j] == 'O') { t++; } if (g[i][j] == 'A') { DP[i][j] = 1; } } } for (int i = 0; i < t/2; i++) { int a, b, c, d; cin >> a >> b >> c >> d; a--; b--; c--; d--; m[{a,b}] = {c,d}; m[{c,d}] = {a,b}; } double ans = 0; for (int q = 0; q < 20000; q++) { double newDP[21][21]; for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) newDP[i][j] = 0; for (int x = 0; x < N; x++) { for (int y = 0; y < M; y++) { double num_free = 0; for (int k = 0; k < 4; k++) { if (x + dx[k] >= 0 && x + dx[k] < N && y + dy[k] >= 0 && y + dy[k] < M && g[x+dx[k]][y+dy[k]] != '#') num_free++; } for (int k = 0; k < 4; k++) { if (x + dx[k] >= 0 && x + dx[k] < N && y + dy[k] >= 0 && y + dy[k] < M && g[x+dx[k]][y+dy[k]] != '#') { int nx = x + dx[k]; int ny = y + dy[k]; if (g[nx][ny] == '%') { ans += DP[x][y] * 1.0/num_free; } else if (g[nx][ny] == '*') { } else if (g[nx][ny] == 'O') { auto p = m[{nx,ny}]; newDP[p.first][p.second] += DP[x][y] * 1.0/num_free; } else { newDP[nx][ny] += DP[x][y] * 1.0/num_free; } } } } } for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) DP[i][j] = newDP[i][j]; } cout << ans << endl; return 0; }