This page is still under construction.

Parts of this page are still being built. What you see may change.

Lavaspar

Interview

Time limit2sMemory limit512 MB

Summary
Count grid cells that belong to at least one contiguous straight segment (row, column, or diagonal) whose letters form an anagram of some word in the given list.
Level

Medium6 of 10

Topics
Implementation, Brute force, Hash map, String matching
Solved
No attempts yet

Problem

Caça Palavras is a well-known pastime, though it has lost some of its prestige in recent years. The goal of the game is to find words in a grid, where each cell of the grid holds one letter.

Bibika and her brother were playing Caça Palavras, but they soon lost interest, since finding all the words was becoming relatively easy. Because Bibika wanted her brother to spend a little less time on the computer, she searched the internet for games of the same style and came across Caça Lavaspar.

Caça Lavaspar follows the same idea as the famous Caça Palavras. Instead of simply having to find a word in the grid, however, the goal is to find any anagram of the word, which makes the game harder and more interesting. The anagram can be found in a row, a column, or a diagonal.

An anagram of a word is formed by rearranging the letters of the word. Sometimes the anagram is meaningless, but that does not matter. BALO, LOBA, and AOLB are examples of anagrams of the word BOLA.

Bibika noticed that the same cell of the grid could be part of anagrams of different words, and she started calling such cells special cells.

Now she would like to know, given a grid configuration and a collection of words, how many special cells there are.

The image above illustrates the first example, where the collection of words consists of three words: BOLA, CASA, and BOI. The rectangles of each color represent anagrams of different words from the input. The 3 special cells are painted yellow.

Input

The first line contains two integers L and C, which correspond to the number of rows and the number of columns of the grid, respectively.

Then L lines follow, each containing a word with C letters.

After that, the next line contains an integer N, which represents the number of words in the word collection that follows.

Finally, there are N more lines, each containing one word of the collection.

All characters used, both in the grid and in the word collection, are uppercase letters of the English alphabet.

No pair of words in the collection is an anagram of the other.

Output

The output consists of a single line containing the number of special cells.

Constraints

  • 2 ≤ L, C ≤ 40.
  • 2 ≤ N ≤ 20.
  • The number of letters P of each of the N words is in the range 2 ≤ P ≤ min(15, max(L, C)).

Examples3

  1. Example 1

    Input
    4 5
    XBOIC
    DKIRA
    ALBOA
    BHGES
    3
    BOLA
    CASA
    BOI
    
    Expected output
    3
    
  2. Example 2

    Input
    3 3
    AAB
    ABA
    BAA
    2
    ABA
    BBB
    
    Expected output
    3
    
  3. Example 3

    Input
    2 4
    AAAA
    AAAA
    2
    AAA
    BBB
    
    Expected output
    0