#include #define mp make_pair #define pb push_back typedef long long ll; typedef long long llong; typedef long double ld; using namespace std; template void dprint(T begin, T end) { for (auto i = begin; i != end; i++) { cerr << (*i) << " "; } cerr << "\n"; } int n, m, k; ld a[500][500]; string s[30]; int cl[30][30]; int st; int cc; int go[30][30]; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; int nm(int x, int y) { if (x < 0 || y < 0 || x >= n || y >= m) return 0; return 1; } int main() { cin >> n >> m >> k; for (int i = 0; i < n; ++i) cin >> s[i]; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { if (s[i][j] == 'A') { st = cl[i][j] = cc++; } else if (s[i][j] == 'O') { cl[i][j] = cc++; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (s[i][j] == 'A' || s[i][j] == '%' || s[i][j] == 'O' || s[i][j] == '*') go[i][j] = cl[i][j]; else go[i][j] = -1; } } for (int i = 0; i < k; ++i) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; --x1, --y1, --x2, --y2; swap(go[x1][y1], go[x2][y2]); } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (s[i][j] == 'A' || s[i][j] == 'O') { int cnt = 0; int fl = 0; int cur = cl[i][j]; for (int k = 0; k < 4; ++k) { int nx = i + dx[k]; int ny = j + dy[k]; if (!nm(nx, ny) || go[nx][ny] == -1) continue; ++cnt; if (s[nx][ny] == '%') fl += 1; else if (s[nx][ny] == 'A' || s[nx][ny] == 'O') { a[cur][go[nx][ny]] = -1; } } a[cur][cc] = fl; a[cur][cur] = cnt; if (cnt == 0) { a[cur][cur] = 1; } } } } { int n = cc; int now = 0; int gg[500]; for (int i = 0; i < n; ++i) { int bst = now; ld df = 0; for (int j = now; j < n; ++j) { ld df2 = abs(a[j][i]); if (df2 > df) df = df2, bst = j; } if (abs(df) <= 1e-6) { gg[i] = -1; continue; } gg[i] = now; for (int j = 0; j <= n; ++j) swap(a[now][j], a[bst][j]); ld k = a[now][i]; for (int j = 0; j <= n; ++j) a[now][j] /= k; for (int j = 0; j < n; ++j) { if (j == now) continue; ld k = a[j][i]; for (int l = 0; l <= n; ++l) a[j][l] -= k * a[now][l]; } ++now; } if (gg[st] == -1) cout << 0 << "\n"; else { cout.setf(ios::fixed); cout.precision(20); cout << a[gg[st]][n] << "\n"; } } return 0; }