import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { private static int WALL = -1; private static int DEATH = -2; private static int VISITED = -10; private static int EXIT = -100; private static int x; private static int y; private static int n; private static int m; private static int k; private static int deathMove = 0; private static int exitMove = 0; private static int[][] neib = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; public static void main(String[] args) { Scanner in = new Scanner(System.in); n = in.nextInt(); m = in.nextInt(); k = in.nextInt(); String[] maze = new String[n]; for(int i = 0; i < n; i++){ maze[i] = in.next(); } int[][] data = convert(maze); // print(data); for(int a0 = 0; a0 < k; a0++){ int i1 = in.nextInt(); int j1 = in.nextInt(); int i2 = in.nextInt(); int j2 = in.nextInt(); data[i1][j1] = tunel(i2, j2); data[i2][j2] = tunel(i1, j1); if (isDead(data, i1, j1)) { data[i2][j2] = DEATH; } if (isDead(data, i2, j2)) { data[i1][j1] = DEATH; } // if (isExit(data, i1, j1)) { // data[i2][j2] = EXIT; // } // if (isExit(data, i2, j2)) { // data[i1][j1] = EXIT; // } } // System.out.println(); // print(data); calculate(data); } private static void print(int[][] data) { for (int[] r : data) { for (int i : r) { char c = '_'; if (i == WALL) c = '#'; else if (i == DEATH) c = '*'; else if (i > 0) c = 'O'; else if (i == EXIT) c = 'E'; System.out.print(c); } System.out.println(); } } private static boolean isDead(int[][] data, int x, int y) { for (int[] n : neib) if (data[x + n[0]][y + n[1]] != WALL) return false; // at least one is not wall return true; } private static void calculate(int[][] data) { boolean v[][] = new boolean[n + 2][m + 2]; // DFS reachability starting with x, y move(v, data, x, y); // System.out.println("EXIT MOVES:" + exitMove); // System.out.println("DEATH MOVE:" + deathMove); double t = exitMove; double b = deathMove + exitMove; System.out.println(t / b); } private static void move(boolean[][] v, int[][] data, int x, int y) { // System.out.println("CHECK " + x + "," + y); if (data[x][y] == DEATH) { // System.out.println("DEATH"); deathMove++; } else if (data[x][y] == EXIT) { // System.out.println("EXIT"); exitMove++; } else { // System.out.println("PROCEED"); v[x][y] = true; for (int[] n : neib) { if (!v[x + n[0]][y + n[1]] && data[x + n[0]][y + n[1]] != WALL) move(v, data, x + n[0], y + n[1]); } } } private static int tunel(int i, int j) {return i * m + j;} private static int[][] convert(String[] maze) { int[][] result = new int[n + 2][m + 2]; for (int i = 0; i < n + 2; i++) { result[i][0] = WALL; result[i][m + 1] = WALL; } for (int i = 0; i < m + 2; i++) { result[0][i] = WALL; result[n + 1][i] = WALL; } for (int r = 0; r < n; r++) { for (int c = 0; c < m; c++) { char s = maze[r].charAt(c); if (s == '#') result[r + 1][c + 1] = WALL; else if (s == 'A') { x = r + 1; y = c + 1; } else if (s == '*') result[r + 1][c + 1] = DEATH; else if (s == '%') result[r + 1][c + 1] = EXIT; } } return result; } }