#include using namespace std; const int maxn = 20 + 5; inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;} long double f[450][450]; const long double eps = 1e-8; void Gauss( long double A[450][450] , int n) { int i, j, k, r; for(i = 0; i < n; ++ i) { r = i; for(j = i+1; j < n; ++ j) if (fabs(A[j][i]) > fabs(A[r][i])) r = j; if(fabs(A[r][i]) < eps) continue; if(r != i) for(j = 0; j <= n; j++) swap(A[r][j], A[i][j]); for(k = 0; k < n; k++) if(k != i) for(j = n; j >= i; j--) A[k][j] -= A[k][i]/A[i][i] * A[i][j]; } for(i = 0 ; i < n ; ++ i) { if(fabs(A[i][n]) < eps) A[i][n] = 0; else A[i][n] /= A[i][i]; } } int n , m , L , dir[4][2] = {-1,0,1,0,0,-1,0,1} , vis[maxn][maxn] , tot , id[maxn][maxn] , ISEnd[maxn * maxn]; vector < pair < pair < int , int > , double > > nxt[maxn][maxn]; pair < int , int > trans[maxn][maxn] , st; vector < pair < int , int > > elf; char s[maxn][maxn]; inline bool inmap( int x , int y ){ return 1 <= x && x <= n && 1 <= y && y <= m; } void BFS(){ queue < pair < int , int > > que; que.push( st ); vis[st.first][st.second] = 1; while( !que.empty() ){ pair < int , int > ff = que.front() ; que.pop(); int x = ff.first , y = ff.second , cnt = 0; if( s[x][y] == '*' ) continue; for(int i = 0 ; i < 4 ; ++ i){ int newx = x + dir[i][0]; int newy = y + dir[i][1]; if( !inmap( newx , newy ) || s[newx][newy] == '#' ) continue; ++ cnt; } for(int i = 0 ; i < 4 ; ++ i){ int newx = x + dir[i][0]; int newy = y + dir[i][1]; if( !inmap( newx , newy ) || s[newx][newy] == '#' ) continue; if( trans[newx][newy].first ){ pair < int , int > temp = make_pair( newx , newy ); newx = trans[temp.first][temp.second].first; newy = trans[temp.first][temp.second].second; } nxt[x][y].emplace_back( make_pair( make_pair( newx , newy ) , 1.0 / ( double ) cnt ) ); if( vis[newx][newy] ) continue; vis[newx][newy] = 1; que.push( make_pair( newx , newy ) ); } } } int main( int argc , char * argv[] ){ n = read() , m = read() , L = read(); for(int i = 1 ; i <= n ; ++ i) scanf( "%s" , s[i] + 1 ); for(int i = 1 ; i <= L ; ++ i){ int x1 = read() , y1 = read() , x2 = read() , y2 = read(); trans[x1][y1] = make_pair( x2 , y2 ); trans[x2][y2] = make_pair( x1 , y1 ); } for(int i = 1 ; i <= n ; ++ i) for(int j = 1 ; j <= m ; ++ j) if( s[i][j] == 'A' ) st = make_pair( i , j ); else if( s[i][j] == '%' ) elf.emplace_back( make_pair( i , j ) ); BFS(); int ok = 0; for(auto it : elf) ok |= vis[it.first][it.second]; if( !ok ){ printf( "%.6lf\n" , 0 ); return 0; } int tot = 1; for(int i = 1 ; i <= n ; ++ i) for(int j = 1 ; j <= m ; ++ j){ if( s[i][j] == 'A' ) continue; id[i][j] = tot ++; if( s[i][j] == '%' || nxt[i][j].size() == 0 ){ ISEnd[tot - 1] = 1; // cout << i << " " << j << endl; } } for(int i = 0 ; i < tot ; ++ i) f[i][i] = 1; for(int i = 1 ; i <= n ; ++ i) for(int j = 1 ; j <= m ; ++ j){ if( s[i][j] == '#' || s[i][j] == '*' || s[i][j] == '%' ) continue; for(auto it : nxt[i][j]){ int v = id[it.first.first][it.first.second]; if(v) f[v][id[i][j]] -= it.second; } } for(int i = 0 ; i < tot ; ++ i){ f[0][i] = 0; if( ISEnd[i] ){ f[0][i] = 1; } } f[0][tot] = 1; Gauss( f , tot ); double ans = 0; for(auto it : elf) ans += f[id[it.first][it.second]][tot]; printf("%.8lf\n" ,ans); return 0; }