Bimatching
Time limit2sMemory limit512 MB
Given a grid of which cavaliers can dance with which ladies, find the maximum number of disjoint triples, each one cavalier with two compatible ladies.
Statement
In the 19th century, dancing was one of the most important subjects that nobles learned at school. Balls were the main, and sometimes the only, chance to meet new people, both for romance and for business. However, many cavaliers died in wars, so a shortage of cavaliers at balls was common. To solve this, some dances were changed so that one cavalier asks two ladies for a dance instead of one. New dances were made for sets of triples, and ordinary dances stayed with sets of pairs.
Nowadays some communities rebuild historical dances, and there are still usually fewer cavaliers than ladies. So dances for lady-cavalier-lady triples are still studied.
Betty leads a popular community of historical dances. Some cavaliers and ladies dislike each other and do not want to dance together in the same triple. Betty knows all of these dislikes. She needs only a way to find the maximum number of triples that can be selected for a dance. She does not need to know the actual bimatching between cavaliers and ladies. She tells the dancers, "Now form triples, please. I know it is possible." Help Betty find the maximum lady-cavalier-lady bimatching capacity.
Input
The first line contains the number of test cases (). Each test case begins with a line containing the number of cavaliers and the number of ladies (, ). The next lines each contain a string of characters, each 0 or 1. The character at position in row is 1 if and only if lady can be in one triple with cavalier . The sum of over all test cases does not exceed 150.
Output
For each test case, output the maximum number of triples that can be formed, on a single line.
Hint
In the illustration, each line means that a cavalier can dance with a lady. A bold line means that a cavalier asks a lady for a dance.