Ancient Messages#2283

Ancient Messages

3 seconds
256 megabytes
Hard

In order to understand early civilizations, archaeologists often study texts written in ancient languages. One such language, used in Egypt more than years ago, is based on characters called hieroglyphs. Figure shows six hieroglyphs and their names. In this problem, you will write a program to recognize these six characters.

Figure : Six hieroglyphs

Input

The input consists of several test cases, each of which describes an image containing one or more hieroglyphs chosen from those shown in Figure . The image is given as a sequence of horizontal scan lines consisting of black pixels (represented by ) and white pixels (represented by ). In the input data, each scan line is encoded in hexadecimal notation. For example, the sequence of pixels (one black pixel, followed by two white pixels, and so on) is represented in hexadecimal as . Only digits and lowercase letters through are used in the hexadecimal encoding. The first line of each test case contains two integers and . Here (0 < H \leq 200) is the number of scan lines in the image, and (0 < W \leq 50) is the number of hexadecimal characters in each line. The following lines contain the hexadecimal characters of the image, from top to bottom. Input images satisfy the following requirements:

  • The image contains only hieroglyphs shown in Figure .

  • Each image contains at least one hieroglyph.

  • Each black pixel is part of a hieroglyph.

  • Each hieroglyph consists of a connected set of black pixels, each of which has at least one adjacent black pixel above, below, to the left, or to the right.

  • Hieroglyphs do not touch each other, and no hieroglyph is inside another.

  • If two black pixels are diagonally adjacent, there is also a black pixel that is adjacent to both.

  • The hieroglyphs may be distorted, but each is topologically equivalent to one of the characters in Figure (two figures are topologically equivalent if one can be transformed into the other by stretching without tearing).

The last test case is followed by a line containing two zeros.

Output

For each test case, print its case number followed by a string containing a single letter for each recognized hieroglyph in the image, using the following codes:

Ankh: A Wedjat: J Djed: D Scarab: S Was: W Akhet: K

In each output string, print the codes in alphabetical order. Follow the format of the sample output.

The sample input contains test cases shown in Figures and . Due to space constraints, not all sample input is provided here.

Examples

Example 1

Loading
Loading