Word Search in a Grid

Decide whether a word appears in a grid along a straight line of neighboring cells in any of the eight directions.

Medium4ArraySimulationImplementationBrute forceInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

An N×MN \times M grid holds one uppercase letter in each cell. Given a word SS, write a program that decides whether the word SS is in the grid.

The word SS is in the grid when you can start at one cell and read the letters of SS in order through neighboring cells. The direction can be up, down, left, right, or one of the four diagonals: upper left, upper right, lower left, lower right. The direction must not change partway.

Input

The first line contains the word SS. The length of SS is at most 100, and SS consists of uppercase letters only.

The second line contains the number of rows NN and the number of columns MM of the grid. NN and MM are natural numbers at most 100.

Each of the next NN lines contains the MM letters of one row of the grid, with no spaces.

Output

Print 1 if the word SS is in the given grid, and 0 if it is not.