Under construction

Still being built. Things may change

Bicube

Time limit8sMemory limit512 MB

Summary
Decide whether eight unit cubes folded from a painted paper sheet can form a 2x2x2 bicube with black inner faces and distinct uniform outer faces.
Level

Medium6 of 10

Topics
Simulation, Implementation, Brute force
Solved
No attempts yet

Problem

Mary Thomas has a number of sheets of squared paper. Some of the squares are painted either in black or in some colorful color (such as red and blue) on the front side. Cutting off the unpainted part, she will have eight opened-up unit cubes. A unit cube here refers to a cube of which each face consists of one square.

She is going to build those eight unit cubes with the front side exposed and then a bicube with them. A bicube is a cube of the size 2 × 2 × 2, consisting of eight unit cubes, that satisfies the following conditions:

  • faces of the unit cubes that come to the inside of the bicube are all black;
  • each face of the bicube has a uniform colorful color; and
  • the faces of the bicube have colors all different.

Your task is to write a program that reads the specification of a sheet of squared paper and decides whether a bicube can be built with the eight unit cubes resulting from it.

Input

The input contains the specification of a sheet. The first line contains two integers H and W, which denote the height and width of the sheet (3 ≤ H, W ≤ 50). Then H lines follow, each consisting of W characters. These lines show the squares on the front side of the sheet. A character represents the color of a grid: alphabets and digits ('A' to 'Z', 'a' to 'z', '0' to '9') for colorful squares, a hash ('#') for a black square, and a dot ('.') for an unpainted square. Each alphabet or digit denotes a unique color: squares have the same color if and only if they are represented by the same character.

Each component of connected squares forms one opened-up cube. Squares are regarded as connected when they have a common edge. Squares that share only a common vertex are not connected.

Output

Print "Yes" if a bicube can be built with the given sheet, and "No" otherwise.

Examples3

  1. Example 1

    Input
    3 40
    .a....a....a....a....f....f....f....f...
    #bc#.#cd#.#de#.#eb#.#cb#.#dc#.#ed#.#be#.
    .#....#....#....#....#....#....#....#...
    
    Expected output
    Yes
    
  2. Example 2

    Input
    5 35
    .a....a....a....a....f....f....f...
    #bc#.#cd#.#de#.#eb#.#cb#.#dc#.#ed#.
    .#..f.#....#....#....#....#....#...
    ..e##..............................
    .b#................................
    
    Expected output
    Yes
    
  3. Example 3

    Input
    3 40
    .a....a....a....a....f....f....f....f...
    #bc#.#cd#.#de#.#eb#.#cb#.#dc#.#ed#.#eb#.
    .#....#....#....#....#....#....#....#...
    
    Expected output
    No