// Arup Guha
// 2/17/2016
// Solution to 2016 FHSPS Playoff Question: Correct Answer Recovery

import java.util.*;

public class recovery {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();

		// Process each case.
		for (int loop=0; loop<numCases; loop++) {

			// Read in the data.
			int numQuestions = stdin.nextInt();
			int numStudents = stdin.nextInt();
			int[] answers = new int[numStudents];

			// Store each set of answers as a bitmask 0 = false, 1 = true;
			for (int i=0; i<numStudents; i++)
				answers[i] = convert(stdin.next());

			// Read in the frequency chart of results.
			int[] freq = new int[numQuestions+1];
			for (int i=0; i<=numQuestions; i++)
				freq[i] = stdin.nextInt();

			int res = 0;

			// Try each possible set of answers and see if it's possible.
			for (int i=0; i<(1<<numQuestions); i++) {

				// Calculate and update the # of questions each student would get
				// if i represented the mask of the correct answers.
				int[] thisFreq = new int[numQuestions+1];
				for (int j=0; j<numStudents; j++)
					thisFreq[numQuestions-Integer.bitCount(i^answers[j])]++;

				// Add 1 to the count if this mask creates the correct frequencies.
				res += possible(thisFreq, freq);
			}

			// Output the result.
			System.out.println(res);
		}
	}

	// Return the binary equivalent of ans, where 'T' = 1, 'F' = 0.
	public static int convert(String ans) {

		// Build up the answer, bit by bit, shifting over current result and placing next bit.
		int res = 0;
		for (int i=0; i<ans.length(); i++) {
			res = (res << 1);
			if (ans.charAt(i) == 'T') res++;
		}

		// Here is our answer.
		return res;
	}

	// Returns 1 iff freq1 and freq2 are identical arrays. Assumes they are equal lengths.
	public static int possible(int[] freq1, int[] freq2) {

		// See if any corresponding values are not equal.
		for (int i=0; i<freq1.length; i++)
			if (freq1[i] != freq2[i])
				return 0;

		// If we made it here, the arrays are equal.
		return 1;
	}
}