#define _CRT_SECURE_NO_DEPRECATE
#define NOFOOTER
#define EJUDGE

#include "testlib.h"

#include <vector>
#include <cassert>

using namespace std;

const int nmax = 701;

int n, m, b;
bool a[nmax][nmax];
bool can[nmax*nmax];
vector < int > c[nmax*nmax];

int main(int argc, char * argv[]) {
    registerTestlibCmd(argc, argv);
    n = inf.readInt();
    m = inf.readInt();
    memset(can, 0, sizeof(can));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            b = inf.readInt() - 1;
            assert(b >= 0 && b < n*m);
            c[b].push_back(i);
            c[b].push_back(j);
            a[i][j] = false;
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            b = inf.readInt() - 1;
            assert(b >= 0 && b < n*m);
            c[b].push_back(i);
            c[b].push_back(j);
            can[b] = true;
        }
    }

    if (ouf.curChar() == 'N') {
        if (ouf.readString() != ans.readString()) quit(_wa, "");
        quit(_ok, "");
    }
    if (ouf.readString() != "YES") quit(_pe, "");
    int k = n*m/2, p;
    for (int i = 0; i < k; ++i) {
        p = ouf.readInt() - 1;

        if (p < 0 || p >= n*m) quit(_wa, "");
        if (!can[p]) quit(_wa, "");
        if (a[c[p][0]][c[p][1]] || a[c[p][2]][c[p][3]]) quit(_wa, "");
        a[c[p][0]][c[p][1]] = a[c[p][2]][c[p][3]] = true;
    }
    if (ans.readString() != "YES") quit(_fail, "");

    quit(_ok, ""); 
}
