#P3585. Stamp Printing Puzzle

    ID: 16838 Type: Default 1000ms 256MiB

Stamp Printing Puzzle

Stamp Printing Puzzle

You are given an n×m grid paper. Some cells must be printed black while the remaining stay white. You also have an a×b stamp with a fixed pattern: some cells on the stamp are raised (will pick up ink) while the others are not.

You need to decide whether it is possible to reproduce the given pattern on the paper using one or more stamp applications under the following conditions:

  1. The stamp cannot be rotated.
  2. The stamp must be fully inside the grid when applied (i.e. no inking outside the paper).
  3. The same cell on the paper cannot be inked (stamped) more than once.

The stamp, when applied at a certain position, will ink exactly the cells corresponding to the raised parts. Formally, if the stamp is applied with its top-left corner at cell (i,j) of the paper then for each cell (r, c) with 0 \le r < a and 0 \le c < b, if the stamp cell is raised then the paper cell (i+r, j+c) gets inked. Also, the paper cell must be intended to be black in the target pattern. The union of cells inked by all stamp placements must exactly equal the target set of black cells.

In addition, note that a stamp application always prints all raised cells in its stamp pattern. In mathematical notation, if a stamp has raised cells at positions \( (r,c) \) where \( stamp[r][c] = 1 \) (the raised ones) then a stamp placed at position \( (i,j) \) inks cells \( (i+r, j+c) \) and it is required that for each such cell the target grid \( T \) satisfies \( T_{i+r,j+c} = 1 \).

inputFormat

The first line contains two integers n and m (the dimensions of the paper).

The next n lines each contain a string of length m consisting only of the characters '#' and '.', where '#' indicates a black cell and '.' indicates a white cell.

The following line contains two integers a and b (the dimensions of the stamp).

The next a lines each contain a string of length b consisting of '#' and '.', where '#' indicates a raised (inked) cell of the stamp and '.' indicates a flat cell.

outputFormat

Output a single line containing YES if it is possible to exactly produce the target pattern under the prescribed conditions; otherwise, output NO.

sample

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