#P9792. [NERC 2018] Bimatching

[NERC 2018] Bimatching

Background

Translated from Problem B of NERC 2018.

Problem Description

You are holding a dance party with some friends.

At this party, there are nn men and mm women. Originally, the dance is done in male-female pairs, but because there are not enough men, you cannot give every woman a male partner. So you invent a new dance form: one man pairs with two female partners.

Of course, when choosing partners, each woman gives a rating to each man. If the rating is 11, it means the woman is willing to dance with that man. Only when both women are willing to dance with that man can they form a partner group.

As the organizer, you need to find the maximum number of such partner groups that can be formed, with the requirement that no person can appear in more than one group.

Input Format

The first line contains an integer t(1t20)t (1 \leq t \leq 20), the number of test cases.

Then follow tt test cases. In each test case, the first line contains two integers nn and mm. It is guaranteed that 1n,m1 \leq n,m and n+m150n + m \leq 150.

Then follows an n×mn \times m matrix, where ai,ja_{i,j} indicates whether woman jj is willing to dance with man ii.

Output Format

For each test case, output one line containing the maximum number of partner groups that can be formed.

2
2 3
111
111
3 4
0110
1100
0011
1
2
1
3 6
001100
111111
001100
2

Hint

The Constraints guarantee 1t201 \leq t \leq 20, 1n,m1 \leq n, m, and n+m150n + m \leq 150.

The figures below explain Sample 1 and Sample 2, where the bold parts show one feasible solution.

Sample 1:

Sample 2:

Translated by ChatGPT 5