Decide whether two binary images represent the same character by comparing their connected components and the containment (surrounds) relations among them.
Medium7GraphBFSDFSImplementationNo attempts yetTime limit2sMemory limit512 MBA syndicate of unknown origin left image data behind, and you have to analyze it. Its members wrote with characters they invented themselves. One binary image holds one character written in black ink on white paper.
Images that look different often stand for the same character. The surrounding relation between connected components decides whether two images stand for the same character. The definitions follow. Assume that white pixels fill the whole plane outside the given image.
![]() | ![]() |
|---|---|
| connected | disconnected |
Connectedness of white pixels.
![]() | ![]() |
|---|---|
| connected | connected |
Connectedness of black pixels.
Let C1 be a connected component of an image, and let C2 be another connected component of the same image with the opposite color. Build a modified image in which every pixel that lies in neither C1 nor C2 takes the color of C2. If neither C1 nor C2 is the background component, the pixels outside the image also take the color of C2. When no pixel of C2 belongs to the background component of the modified image, C1 surrounds C2 in the original image.

Two images stand for the same character when both of the following conditions hold.
The connected components of the two images in the figure below have the following surrounding relations.
The bijection f(Ci)=Ci′ satisfies both conditions, so the two images stand for the same character.


Write a program that decides whether two given images stand for the same character.
The input holds at most 200 datasets. A line with two zeros ends the input. Each dataset has this form.
image 1
image 2
Each image has this form.
h w
p(1,1) ... p(1,w)
...
p(h,1) ... p(h,w)
h and w are the height and the width of the image in pixels, and 1≤h≤100 and 1≤w≤100 hold. Each of the h lines that follow holds exactly w characters with no separator between them. p(y,x) is the color of the pixel in row y from the top and column x from the left. A period (".") means white and a sharp sign ("#") means black.
For each dataset, print yes on one line when the two images stand for the same character, and print no otherwise. Print nothing else.