import java.util.*; import java.io.*; public class tiling { private static class Domino { public Domino(int x, int y) { this.x = x; this.y = y; } int x, y; } private static class Position { public Position(int x, int y, int u, int o) { this.x = x; this.y = y; dx = u; dy = o; } int x, y; int dx, dy; } private static boolean first; private static int count; private static int map[][]; private static char charMap[][]; private static List tiles; private static List> positions; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int width, height, num_tiles; StringTokenizer st; int a, b, k, origA, origB, stepsA, stepsB, meet; boolean done; while (true) { st = new StringTokenizer(br.readLine()); height = Integer.parseInt(st.nextToken()); width = Integer.parseInt(st.nextToken()); num_tiles = Integer.parseInt(st.nextToken()); if(height == 0 && width == 0 && num_tiles==0) break; tiles = new ArrayList(); st = new StringTokenizer(br.readLine()); map = new int[height + 2][width + 2]; charMap = new char[height + 2][width + 2]; while (st.hasMoreTokens()) { tiles.add(new Domino(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()))); } int i, j; for (i = 1; i <= height; i++) { st = new StringTokenizer(br.readLine()); for (j = 1; j <= width; j++) { char c = st.nextToken().charAt(0); if (c == 'X') { charMap[i][j] = 'X'; map[i][j] = -1; } else { map[i][j] = Character.getNumericValue(c); } } map[i][0] = -1; map[i][width + 1] = -1; } for (j = 0; j < width+2; j++) { map[0][j] = -1; map[height + 1][j] = -1; } positions = new ArrayList>(); for (Domino d : tiles) { List possible_positions = new ArrayList(); for (i = 1; i <= height; i++) { for (j = 1; j <= width; j++) { if (map[i][j] == d.x) { if (map[i][j + 1] == d.y) { possible_positions.add(new Position(i, j, 0, 1)); } if (map[i][j - 1] == d.y && d.x != d.y) { possible_positions.add(new Position(i, j, 0, -1)); } if (map[i + 1][j] == d.y) { possible_positions.add(new Position(i, j, 1, 0)); } if (map[i - 1][j] == d.y && d.x != d.y) { possible_positions.add(new Position(i, j, -1, 0)); } } } } positions.add(possible_positions); } first = true; count = 0; place_tile(0); if(count == 0){ System.out.println("impossible"); }else{ System.out.println(count-1); } System.out.println(); br.readLine(); } } public static void place_tile(int i) { if (i == tiles.size()) { if (first) { for (int j = 1; j < map.length - 1; j++) { for (int k = 1; k < map[0].length - 1; k++) { System.out.print(charMap[j][k]); } System.out.println(); } } first = false; count++; return; } for (Position p : positions.get(i)) { char x = charMap[p.x][p.y]; char y = charMap[p.x + p.dx][p.y + p.dy]; if (x == '[' || x == ']' || x == 'n' || x == 'u') { continue; } if (y == '[' || y == ']' || y == 'n' || y == 'u') { continue; } if (p.dx == 0) { if (p.dy == -1) { charMap[p.x][p.y - 1] = '['; charMap[p.x][p.y] = ']'; } else { charMap[p.x][p.y] = '['; charMap[p.x][p.y + 1] = ']'; } } else { if (p.dx == -1) { charMap[p.x - 1][p.y] = 'n'; charMap[p.x][p.y] = 'u'; } else { charMap[p.x][p.y] = 'n'; charMap[p.x + 1][p.y] = 'u'; } } place_tile(i + 1); charMap[p.x][p.y] = 'H'; charMap[p.x + p.dx][p.y + p.dy] = 'H'; } } }