#C6012. Find Hello Path in a Matrix

    ID: 49726 Type: Default 1000ms 256MiB

Find Hello Path in a Matrix

Find Hello Path in a Matrix

You are given an N x M matrix of characters. Your task is to determine if there exists a path in the matrix that spells out the word HELLO. You can start at any cell that contains the letter 'H' and move to any adjacent cell (horizontally, vertically, or diagonally) to find the subsequent letters. The same cell cannot be used more than once in the formation of the word.

The movement is allowed in all 8 directions. Formally, if you are at cell (i,j), you may move to any cell (i + \Delta i, j + \Delta j) where \(\Delta i, \Delta j \in \{-1, 0, 1\}\) and \((\Delta i, \Delta j) \neq (0, 0)\).

Determine if such a path exists that spells out the word \(HELLO\).

Example:

Input:
3 4
HAPE
ELLO
KXYZ

Output: YES

</p>

inputFormat

The first line contains two integers \(N\) and \(M\), representing the number of rows and columns of the matrix respectively. Each of the following \(N\) lines contains a string of length \(M\) representing a row of the matrix.

Input Format:

N M
row_1
row_2
... 
row_N

outputFormat

Output a single line containing "YES" if there exists a path that spells out the word \(HELLO\), otherwise output "NO".

Output Format:

YES or NO
## sample
3 4
HAPE
ELLO
KXYZ
YES