#GYM104598J. Special Word Search

Special Word Search

Description

A word search is a puzzle in which the objective is to find certain words hidden within a rectangular grid of letters. Words must be in a straight line, meaning that they are horizontal, vertical, or diagonal.

A special word search is a word search that allows words to wrap around from one side of the word search to the opposite side. Like a regular word search, words must be in a straight line (horizontal, vertical, or diagonal). The same spot in the word search cannot be used in the same word twice.

For your computer science class final project, you decide to build and train an AI model to detect words in a special word search. However, you must label pieces of training data for the model. To do this, for every given $N$ x $N$ board of the special word search and an array of $K$ words, you must determine how many of those words appear in the word search.

Note that there may be words contained inside other words, such as "sea" and "seashell". There may also be multiple occurrences of the same word, but each word should only be counted once.

$1 ≤ N ≤ 100$

$1 ≤ K ≤ 5,000$

$1 ≤ $ words[$i$].length $≤ 100$

Line 1: One integer representing the number of given words that appear in the special word search.

Input

$1 ≤ N ≤ 100$

$1 ≤ K ≤ 5,000$

$1 ≤ $ words[$i$].length $≤ 100$

Output

Line 1: One integer representing the number of given words that appear in the special word search.

4 5
t h i s
b a r c
w t m e
a p s o
this sea soap her water
3

Note

t h i s

b a r c

w t m e

a p s o

Note that "her" and "soap" wrap around the word search.