public static void main(String str[]) throws IOException {
        Locale.setDefault(Locale.US);

        final int size = 4;

        String line = "";
        int grid[][] = new int[size][size];
        for (int i = 0; i < size; i++) {
            line = buffer.readLine();
            for (int j = 0; j < size; j++) {
                if (((Character) line.charAt(j)).equals('X')) {
                    grid[i][j] = 1;
                }
            }
        }

        Character[][] password = new Character[size][size];

        for (int i = 0; i < size; i++) {
            line = buffer.readLine();
            for (int j = 0; j < size; j++) {
                password[i][j] = line.charAt(j);
            }
        }

        String spassword = "";
        for (int next = 0; next < 4; next++) {
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    if (grid[i][j] == 1) {
                        spassword += password[i][j];
                    }
                }
            }
            grid = turnRight(grid, size);
        }

        out.println(spassword);
        out.flush();
    }