Problem:
Deciphering Characters

Image data which are left by a mysterious syndicate were discovered.
You are requested to analyze the data.
The syndicate members used characters invented independently.
A binary image corresponds to one character written in black ink on white paper.

Although you found many variant images that represent the same character,
you discovered that you can judge whether or not two images represent the same character with the surrounding relation of connected components. We present some definitions as follows. Here, we assume that white pixels fill outside of the given image.

White connected component : A set of white pixels connected to each other horizontally or vertically (see below).

Black connected component : A set of black pixels connected to each other horizontally, vertically, or diagonally (see below).

Connected component : A white or a black connected component.

Background component : The connected component including pixels outside of the image. Any white pixels on the periphery of the image are thus included in the background component.

connected

disconnected

connected

connected

Connectedness of white pixels     

Connectedness of black pixels

Let C1 be a connected component in an image and C2 be another connected component in the same image with the opposite color.
Let's think of a modified image in which colors of all pixels not included in C1 nor C2 are changed to that of C2. If neither C1 nor C2 is the background component, the color of the background component is changed to that of C2. We say that C1 surrounds C2 in the original image when pixels in C2 are not included in the background component in the modified image. (see below)

Two images represent the same character if both of the following conditions are satisfied.

The two images have the same number of connected components.

Let S and S' be the sets of connected components of the two images. A bijective function f : S → S' satisfying the following conditions exists.

For each connected component C that belongs to S, f (C) has the same color as C.

For each of C1 and C2 belonging to S, f (C1) surrounds f (C2) if and only if C1 surrounds C2.

Let's see an example. Connected components in the images of the figure below has the following surrounding relations.

C1 surrounds C2.

C2 surrounds C3.

C2 surrounds C4.

C'1 surrounds C'2.

C'2 surrounds C'3.

C'2 surrounds C'4.

A bijective function defined as f (Ci) = C'i for each connected component satisfies the conditions stated above. Therefore, we can conclude that the two images represent the same character.

Make a program judging whether given two images represent the same character.

Input

The input consists of at most 200 datasets. The end of the input is represented by a line containing two zeros. Each dataset is formatted as follows.

image 1

image 2

Each image has the following format.

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 numbers of pixels.
You may assume that 1 ≤ h ≤ 100 and 1 ≤ w ≤ 100.

Each of the following h lines consists of w characters.
p(y,x) is a character representing the color
of the pixel in the y-th row from the top and the x-th column from the left.
Characters are either a period (".") meaning white or a sharp sign ("#") meaning black.

Output

For each dataset, output "yes" if the two images represent the same character, or output "no", otherwise, in a line. The output should not contain extra characters.

Sample Input

3 6
.#..#.
#.##.#
.#..#.
8 7
.#####.
#.....#
#..#..#
#.#.#.#
#..#..#
#..#..#
.#####.
...#...
3 3
#..
...
#..
3 3
#..
.#.
#..
3 3
...
...
...
3 3
...
.#.
...
3 3
.#.
#.#
.#.
3 3
.#.
###
.#.
7 7
.#####.
#.....#
#..#..#
#.#.#.#
#..#..#
#.....#
.#####.
7 7
.#####.
#.....#
#..#..#
#.#.#.#
#..#..#
#..#..#
.#####.
7 7
.#####.
#.....#
#..#..#
#.#.#.#
#..#..#
#.....#
.#####.
7 11
.#####.....
#.....#....
#..#..#..#.
#.#.#.#.#.#
#..#..#..#.
#.....#....
.#####.....
7 3
.#.
#.#
.#.
#.#
.#.
#.#
.#.
7 7
.#####.
#..#..#
#..#..#
#.#.#.#
#..#..#
#..#..#
.#####.
3 1
#
#
.
1 2
#.
0 0

Output for the Sample Input

yes
no
no
no
no
no
yes
yes

Solution in C++14 (judge verdict: Wrong Answer)

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <random>
#include <set>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; ++i)
typedef  long long ll;
class UF {
private:
    int sz; vector<int> par,nrank,size;
public:
    UF(){}
    UF(int node_size){ sz = node_size; par.resize(sz),nrank.resize(sz),size.resize(sz);
        rep(i,sz){ par[i] = i; nrank[i] = 0; size[i] = 1;} }
    int find(int x){ if(par[x] == x){ return x; }else{ return par[x] = find(par[x]); } }
    void unite(int x,int y)
    { x = find(x),y = find(y); if(x == y) return; if(nrank[x] < nrank[y]) swap(x,y);
        par[y] = x; size[x] += size[y]; if(nrank[x] == nrank[y]) nrank[x]++; }
    int query(int x){ x = find(x); return size[x]; }
    bool same(int x,int y){ return find(x) == find(y); }
};

UF ca(vector<string> &s){
    int h = s.size();
    int w = s[0].size();
    UF uf(h*w);
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(s[i][j]=='.'){
                if(i>0&&s[i-1][j]=='.'){
                    uf.unite(i*w+j,(i-1)*w+j);
                }
                if(i<h-1&&s[i+1][j]=='.'){
                    uf.unite(i*w+j,(i+1)*w+j);
                }
                if(j>0&&s[i][j-1]=='.'){
                    uf.unite(i*w+j,(i)*w+j-1);
                }
                if(j<w-1&&s[i][j+1]=='.'){
                    uf.unite(i*w+j,(i)*w+j+1);
                }
            }else{
                for(int a=-1;a<2;a++){
                    for(int b=-1;b<2;b++){
                        if(a==0&&b==0)continue;
                        if(i+a>=0&&i+a<h&&j+b>=0&&j+b<w&&s[i][j]==s[i+a][j+b]){
                            uf.unite(i*w+j,(i+a)*w+j+b);
                        }
                    }
                }
            }
        }
    }
    return uf;
}

string bp(vector<vector<int> > &v,int i){
    if(v[i].size()==0)return "()";
    vector<string> s;
    for(auto x:v[i]){
        s.push_back(bp(v,x));
    }
    sort(s.begin(),s.end());
    string ans = "(";
    for(auto x:s){
        ans +=x;
    }
    ans += ")";
    return ans;
}

string calc(vector<string>s){
    int h = s.size();
    int w = s[0].size();
    UF uf = ca(s);
    map<int,int>mp;
    int cnt = 0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(mp.find(uf.find(i*w+j))==mp.end()){
                mp[uf.find(i*w+j)] = cnt;
                cnt++;
            }
        }
    }
    vector<vector<int> > v(cnt);
    set<pair<int,int> > st;
    for(int i=0;i<h;i++){
        for(int j=0;j<w-1;j++){
            int x = uf.find(i*w+j);
            int y = uf.find(i*w+j+1);
            if(x==y||st.count(make_pair(x,y))==1||st.count(make_pair(y,x))==1){
                continue;
            }
            if(x==uf.find(0)){
                v[mp[x]].push_back(mp[y]);
            }else{
                vector<string> s2 = s;
                for(int p=0;p<h;p++){
                    for(int q=0;q<w;q++){
                        if(uf.find(p*w+q)!=x&&uf.find(p*w+q)!=y){
                            s2[p][q] = s[i][j+1];
                        }
                    }
                }
                UF uf2 = ca(s2);
                if(uf2.find(0)!=uf2.find(i*w+j+1)){
                    v[mp[x]].push_back(mp[y]);
                }
            }
            st.insert(make_pair(x,y));
        }
    }
    return bp(v,mp[uf.find(0)]);
}



int main(){
    int h1,w1,h2,w2;
    while(cin >> h1 >> w1 &&h1!=0){
        vector<string>s1(h1+2);
        for(int i=0;i<h1;i++)cin >> s1[i+1];
        for(int i=0;i<w1+2;i++){
            s1[0].push_back('.');
        }
        for(int i=0;i<h1;i++){
            s1[i+1] = "."+s1[i+1] + ".";
        }
        for(int i=0;i<w1+2;i++){
            s1[h1+1].push_back('.');
        }
        string x1 =calc(s1);
        cin >> h2  >> w2;
        vector<string>s2(h2+2);
        for(int i=0;i<h2;i++)cin >> s2[i+1];
        for(int i=0;i<w2+2;i++){
            s2[0].push_back('.');
        }
        for(int i=0;i<h2;i++){
            s2[i+1] = "."+s2[i+1] + ".";
        }
        for(int i=0;i<w2+2;i++){
            s2[h2+1].push_back('.');
        }
        string x2 =calc(s2);
        cout << x1 << endl;
        cout << x2 << endl;
        if(x1==x2){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }
    return 0;
}