Ancient Hieroglyphs

Time limit1sMemory limit192 MB

Problem

Archaeologists study writing in ancient languages to understand early civilizations. About $3000$ years ago, the Egyptians used an ancient script called hieroglyphs, whose characters were modeled on animals, objects, and parts of the body.

In this problem you write a program that recognizes the following six hieroglyphs. Each hieroglyph is a single shape made of connected black pixels, and the six are distinguished by having different numbers of holes (white regions completely enclosed by the shape). In other words, the number of holes alone determines which hieroglyph it is.

HieroglyphCodeHoles
AnkhA1
WedjatJ3
DjedD5
ScarabS4
WasW2
AkhetK0

Determine every hieroglyph that appears in the given image.

Input

The input consists of several test cases. Each test case is a single image containing one or more hieroglyphs. The image is made of $0$s and $1$s, where $1$ is a black pixel and $0$ is a white pixel.

Each line of the image is encoded in hexadecimal. For example, the eight pixels $10011100$ are encoded as 9c. The hexadecimal encoding uses the characters 09 and af.

The first line of each test case contains two integers $H$ and $W$. $H$ ($0 < H \le 200$) is the number of image lines and $W$ ($0 < W \le 50$) is the number of hexadecimal characters on each line, so the image is $4W$ pixels wide. The next $H$ lines give the image.

Each input image satisfies the following rules.

  1. The image contains only the six hieroglyphs described above.
  2. Every image contains at least one valid hieroglyph.
  3. Every black pixel is part of some valid hieroglyph.
  4. A hieroglyph is a connected set of black pixels: every black pixel is adjacent (up, down, left, or right) to at least one other black pixel.
  5. Distinct hieroglyphs do not touch one another, and no hieroglyph is contained inside another.
  6. Whenever two black pixels touch diagonally, there is a black pixel that is orthogonally adjacent to both of them.
  7. A hieroglyph may be somewhat distorted, but its number of holes is always as given in the table above.

The line after the last test case contains two $0$s.

Output

For each test case, print one line in the form Case x: codes, where $x$ is the test case number starting from $1$ and codes is the string of hieroglyph codes appearing in the image, sorted in alphabetical order.