Problem:
Score : 200 points

Problem Statement

You are given an image A composed of N rows and N columns of pixels, and a template image B composed of M rows and M columns of pixels.

A pixel is the smallest element of an image, and in this problem it is a square of size 1×1.

Also, the given images are binary images, and the color of each pixel is either white or black.

In the input, every pixel is represented by a character: . corresponds to a white pixel, and # corresponds to a black pixel.

The image A is given as N strings A_1,...,A_N.

The j-th character in the string A_i corresponds to the pixel at the i-th row and j-th column of the image A (1≦i,j≦N).

Similarly, the template image B is given as M strings B_1,...,B_M.

The j-th character in the string B_i corresponds to the pixel at the i-th row and j-th column of the template image B (1≦i,j≦M).

Determine whether the template image B is contained in the image A when only parallel shifts can be applied to the images.

Constraints

1≦M≦N≦50

A_i is a string of length N consisting of # and ..

B_i is a string of length M consisting of # and ..

Input

The input is given from Standard Input in the following format:

N M
A_1
A_2
:
A_N
B_1
B_2
:
B_M

Output

Print Yes if the template image B is contained in the image A. Print No otherwise.

Sample Input 1

3 2
#.#
.#.
#.#
#.
.#

Sample Output 1

Yes

The template image B is identical to the upper-left 2 × 2 subimage and the lower-right 2 × 2 subimage of A. Thus, the output should be Yes.

Sample Input 2

4 1
....
....
....
....
#

Sample Output 2

No

The template image B, composed of a black pixel, is not contained in the image A composed of white pixels.

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.*;

public class Main {

  public static void main (String[] args) {

    Scanner sc = new Scanner(System.in);

    int N = sc.nextInt();
    int M = sc.nextInt();

    String[][] A = new String[N][N];

    for (int i = 0; i < N; i++) {
      String a = sc.next();
      A[i] = a.split("");
    }

    String[] B = new String[M];

    for (int i = 0; i < M; i++) {
      B[i] = sc.next();
    }

    sc.close();

    String[][] A2 = new String[N][N];

    int maxK = 0;
    int maxI = 0;

    for (int k = 0; k < N; k++) {
      for (int i = 0; i < N - M + 1; i++) {
        String a_ = "";
        for (int j = i; j < i + M; j++) {
          a_ += A[k][j];
        }
        A2[k][i] = a_;
        maxK = Math.max(maxK, k);
        maxI = Math.max(maxI, i);
      }
    }

    String result = "No";

    int cA = 0;
    int rA = 0;
    int cB = 0;

    while (cA <= maxI) {
      while (rA <= maxK) {
        if (A2[rA][cA].equals(B[cB])){
          result = "Yes";
          cB++;
          rA++;
          if (cB == M) {
            rA = maxK + 1;
          }
        } else {
          result = "No";
          rA++;
          cB = 0;
        }
      }
      if (result.equals("Yes")) {
        cA = maxI + 1;
      } else {
        rA = 0;
        cB = 0;
        cA++;
      }
    }

    if (result.equals("Yes") && cB != M) {
      result = "No";
    }

    System.out.println(result);

  }

}