LogiQuest

216. Word Search

MediumDFSBacktracking

Print YES if the word can be formed from letters of sequentially adjacent cells (up, down, left, right) without reusing a cell, else NO.

Input format

Line 1: r c Next r lines: the grid rows Last line: the word

Output format

YES or NO

Example 1

Input

3 4
ABCE
SFCS
ADEE
ABCCED

Output

YES

Example 2

Input

3 4
ABCE
SFCS
ADEE
SEE

Output

YES

Constraints

1 ≤ r, c ≤ 6 1 ≤ |word| ≤ 15

Read input from standard input and print the answer to standard output. Your code is judged against 4 test cases, most of them hidden.

Loading the MyLoginn IDE…