Writer Nguyen Nhu wants to ask you to rely on the law of even and odd tones to build an automatic writing machine model. He has a list of N Vietnamese words. Some pairs of words are allowed to stand next to each other
in the same sentence, while other pairs of words are not. In addition, you are only allowed to start a sentence
with certain words. The end of a sentence is always a period. Each sentence must contain at least
one word. An essay is composed of a number of ordered sentences. An essay without any sentences
is considered an essay.

The given N words are divided into two groups: ugly words and beautiful words. If the last word of a sentence is a beautiful word, then that sentence is called a perfect sentence. If the last word of a sentence is an ugly word, then that sentence
is called an imperfect sentence. If the essay contains an imperfect sentence, then it is only allowed
to be the last sentence in that essay.

Because a sentence should not be too long or too short, writer Nguyen Nhu has determined
in advance an integer K, from which we have the formula to calculate the score of a sentence with w words (w > 0, a period is not counted as a word) as:
K + (K – 1) + (K – 2) + ... + (K – w + 1)
You are also given the concordance/inconcordance relationship between 6 tone marks (level, flat, sharp, asking,
falling, heavy) in the form of a 6 × 6 binary matrix. Each relationship will indicate whether a sentence ending with this tone mark should be placed immediately after a sentence ending with another tone mark. Two sentences
next to each other ending with two concordance tones are called a concordance pair. There is only
one exception: an imperfect sentence will not be considered concordance with the previous sentence, regardless of what tone mark they end with.
Writer Nguyen Nhu wants you to find an algorithm to generate an essay with a length of no more than M words that has the
greatest smoothness. The smoothness of an essay is calculated as follows:
Smoothness = (total score of sentences) × (number of pairs of sentences that match in the essay)
Requirement: Given integers N, K, M, and also given the relationship between words and the relationship between
tones, calculate the smoothness of the essay with the greatest smoothness that is no more than M words long.

Data:
• The first line contains three positive integers N, K, M
• The second line contains N integers in the range 1 to 6, representing the tone marks of the words.
• The next 6 lines, each line contains 6 integers that take on one of two values ​​0 or 1. If
the jth number on the ith line in these 6 lines is 0, then the previous sentence ending with i will
not match the next sentence ending with j. Conversely, if the jth number on the ith line in
these 6 lines is 1, then the previous sentence ending with i will match the next sentence ending with
j.
• The next N + 1 lines, each consisting of N + 1 integers representing a binary matrix
of size (N + 1) × (N + 1). In this matrix:
- If the jth number (1 ≤ j ≤ N) on the ith line (1 ≤ i ≤ N) is 1, then the jth word can be written
immediately after the ith word in the same sentence. Conversely, if the jth number on the ith line is 0, then it will not be allowed to write the jth word immediately after the ith word in the same sentence.
- If the N + 1th number on the ith line (1 ≤ i ≤ N) is 1, then the ith word is a good word, otherwise
the ith word is an ugly word.
- If the i-th number (1 ≤ i ≤ N) on the N + 1th line is 1, then you are allowed to start the sentence
with the i-th word, but if it is 0, then you are not allowed.
- The N + 1th number on the N + 1th line is always 0.
Result: Print a single integer, the smoothness of the text
with the greatest smoothness of no more than M words.
You're a competitive programmer, solve this problem in c++