We have a problem: given N friends and K dishes (K ≤ 10). For each friend i, we have a binary string of length K, where '1' indicates that the friend likes that dish. A friend is happy if at least one dish from the selected set is liked by them (i.e., for that friend, there is some dish j in the selected set such that the j-th character is '1'). We need to select a subset of dishes (minimum size) such that every friend is happy. It is guaranteed that each friend likes at least one dish (so there is always a solution: select all dishes). We need to find the minimum number.
K ≤ 10, so we can use bitmask DP or brute-force over subsets. Since K is small, the number of subsets is 2^K ≤ 1024. For each test case, we can iterate over subsets of dishes (represented as a bitmask of length K). For each subset, check if it satisfies all friends: for each friend, the bitwise AND between the friend's likes mask and the subset mask must be non-zero. If it satisfies, we update the minimum size (popcount). The number of subsets is at most 1024, and N up to 500, T up to 10, so total operations at most 105001024 = ~5 million, which is fine.
We must also consider if there is a solution with less than all dishes. The problem likely expects to find the minimum. However, we must be careful: the answer might be that we need all dishes? But we can compute.
Edge Cases: K=0? But K≥1. N can be up to 500, but small K.
The sample: 2 friends, 2 dishes: friend1: 10 -> likes dish1, friend2: 01 -> likes dish2. To make both happy, we need both dishes, so answer 2.
Thus, a straightforward solution.
But maybe there is a twist: The problem statement says "minimum dishes to order so that all of her N friends are happy which means everyone has at least one dish to eat in party." We want minimum number of dishes to include in the menu. Yes.
We can also think of this as a set cover problem: dishes cover friends (each dish covers a set of friends who like it). But since K is small, we can just brute force.
Implementation details: For each test case, read N, K. Then for each friend, read a string of length K. Convert to integer mask: for each character, if '1', set bit. Then we have an array likes[N] of masks.
Then loop over mask from 1 to (1<<K)-1 (maybe include 0? But 0 would not satisfy any friend since each friend has at least one like, but 0 has no dishes, so it would not satisfy. So start from 1). For each mask, check if for all i from 0 to N-1, (likes[i] & mask) != 0. If yes, compute popcount and keep min.
At end, output min.
Time complexity: O(T * (2^K) * N).
Potential optimization: Preprocess friends? Not necessary.
But note: K ≤ 10, so 2^K <= 1024.
Double-check constraints: N up to 500, T up to 10, so worst-case 105001024 = 5.12e6 operations, which is fine.
We should ensure we handle input correctly: The strings might be given without spaces? In sample: after 2 2, we have "10 " and "01". Possibly each string on its own line, might have spaces? Usually they are given as continuous string. We'll read using input().strip().
Now, output: For each test case, print the answer.
But the sample input is:
1
2 2
10
01
So that's as expected.
I'll implement in Python.
Now, there might be a corner: Could there be a scenario where no subset of dishes makes all friends happy? Since each friend likes at least one dish, selecting all dishes definitely makes all happy, so there's always a solution. So min will be found.
Thus, simple brute force.
But wait: There's note: "Its for sure that everyone has at least liking for one dish." So each friend's mask is non-zero.
Hence, code is straightforward.
But before finalizing, let's think if there's any nuance: The problem says "You need to tell the minimum number of dishes to be taken in menu so that all friends are happy." This is exactly set cover. Since K small, exhaustive search works.
Could there be a more efficient method? For K=10, it's fine.
Edge Cases: N can be large (500) but K small.
Potential pitfall: Input reading: The line with N and K: "N K". Then each next line is a string of length K, but there may be spaces? Probably not. Use strip.
Implementation:
def solve():
import sys
data = sys.stdin.read().strip().split()
# But careful: strings may have no spaces, splitting by whitespace will give each token.
# If strings are like '10' and '01', they will be tokens. So we can parse.
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
N = int(next(it)); K = int(next(it))
likes = []
for i in range(N):
s = next(it).strip()
# If length of s might be less than K? Possibly due to spaces? But we assume correct.
mask = 0
for ch in s:
mask = (mask << 1) | (ch == '1')
# But need to ensure order: Usually first character corresponds to dish 1? If we shift left, then dish1 becomes most significant bit? But order doesn't matter as long as consistent. The mask representation just needs to map dishes to bits. We can simply parse from left to right: dish1 corresponds to bit0? Actually it's easier to assign bit0 to dish1, bit1 to dish2, etc. So if we parse from leftmost char (first) as dish1, we can do: mask = 0; for ch in s: mask <<= 1; if ch == '1': mask |= 1. Then after loop, bit positions: dish1 is most significant? Wait: Suppose s="10", K=2. Then after processing first char '1', mask <<= 1 (mask=0) then if '1': mask|=1 -> mask=1. Then process second char '0': mask<<=1 -> mask=2; if '0': nothing -> mask=2. So final mask=2 (binary 10). That means dish1 is represented by bit1? Actually mask=2 in binary is '10' (bit1 set, bit0 clear). But dish1 corresponds to first character which was '1', we expected bit for dish1 set. But here dish1 is represented by the leftmost character; after shifting, the first character ends up as the most significant bit. So with mask=2 (binary 10), the bit for dish1 is the bit1 (value 2), and dish2 is bit0 (value 1). That's fine, as long as we use consistent mapping. The subsets mask will be formed similarly: we will iterate over masks from 1 to (1<<K)-1, where bit0 corresponds to dish? Actually when we generate subset masks, we assign bits arbitrarily: typically bit0 corresponds to dish1, bit1 to dish2, etc. But our parsing assigns dish1 to the most significant bit if we use left-to-right shifting. That would be inconsistent if we then interpret subset masks as bit0 is dish1. To avoid confusion, it's simpler to parse into mask with dish j (1-indexed) mapping to bit (j-1). So if string s[0] is for dish1, we can set bit0 if s[0]=='1'. So we can do: mask = 0; for idx,ch in enumerate(s): if ch=='1': mask |= (1 << idx). That's safer. So I'll do that: mask = 0; for j,ch in enumerate(s): if ch == '1': mask |= (1 << j). Then dish j corresponds to bit (j-1). Then when we loop over subset mask from 1 to (1<<K)-1, bit j corresponds to dish j+1. So that works.
likes.append(mask)
# Now find min
min_dishes = K # at most K
for mask in range(1, 1<<K):
# quick prune: if popcount >= min_dishes, maybe skip? but not necessary.
ok = True
for like in likes:
if (like & mask) == 0:
ok = False
break
if ok:
cnt = bin(mask).count('1')
if cnt < min_dishes:
min_dishes = cnt
out.append(str(min_dishes))
sys.stdout.write("\n".join(out))
But we need to be careful: The input might be such that the strings are separated by spaces? In sample, they are on separate lines; when we split whitespace, they appear as tokens. So reading via sys.stdin.read().split() works fine. However, the strings might contain no spaces, but if the string has length K, it's one token. So okay.
Now, complexity is fine.
But could there be a scenario where N is 0? N≥1.
Edge: K up to 10, so (1<<K) up to 1024.
Alternatively, we could use a more efficient method: For each subset, we can precompute which friends are covered. But not needed.
Thus, answer.
But wait, the problem statement says: "Each of the N lines contains a string of 0 and 1 where if j th (1 ≤ j ≤ K) value in i th line (1 ≤ i ≤ N) is set 1 then it shows that dish number j is liked by that ith Samu's friend." So indeed, first character is dish1.
Thus, correct.
Now, final code.
But also, there might be multiple test cases. We'll output each answer on new line.
Now, final solution:
We'll implement solve().
One more check: The sample output is 2. Our code should produce 2.
Now, I want to think about potential tricky case: Suppose N=3, K=3, likes: 111, 111, 111. Minimum dishes = 1. Our code will find mask with one bit works.
Suppose likes: 100, 010, 001. Then to satisfy all three, need all three dishes (since each friend only likes one distinct dish). So answer=3.
Suppose likes: 110, 101, 011. Then can we do with 2? Let's test: Subset {1,2} (bits 0 and 1) => friend1 (110) has like (bits 0 and 1 set) -> ok; friend2 (101) likes dish1 and dish3: mask has dish1 -> ok; friend3 (011) likes dish2 and dish3: mask has dish2 -> ok. So answer=2. Our code will find that.
Now, time limit? Not given, but fine.
But there might be a nuance: The problem might be about "minimum dishes" but we need to ensure that no friend is unhappy, i.e., each friend must have at least one liked dish. That's what we did.
Now, write final answer.
But wait: The sample input format: "1\n2 2\n10 \n01\n" There is a space after 10? Possibly not. In the sample it shows "10 " with a space? Actually the sample shows:
SAMPLE INPUT
1
2 2
10
01
There is a trailing space after 10? In many problem statements, they might show the string with a space to indicate that it's separate, but not actual. Usually it's just "10" on a line. We'll treat accordingly.
Now, code.
But I should also consider that reading with sys.stdin.read().split() will treat "10" and "01" as tokens, irrespective of spaces or newlines. That's fine.
Thus, solution.
But before finalize, can we optimize using meet-in-the-middle? Not necessary.
Thus, final answer: brute force.
Now, I'll produce the final code.
Let me write the solution in Python 3.
I'll include a main guard.
Ok.
One more thought: There's a potential for early exit: If we find a subset of size 1 that works, we can break early. So we can iterate over subsets in increasing order of popcount. Since we want minimum, we could generate subsets in order of increasing size. But easier: iterate mask from 1 to (1<<K)-1, compute popcount and update min. But we can early exit if min becomes 1 because can't go lower. However, to early exit we could break out of loops, but we need to break out of the mask loop. But it's fine.
Alternatively, we can generate all masks and sort by popcount, but not needed.
We can also iterate over mask and if bin(mask).count('1') >= min_dishes: continue, to prune. But not necessary.
Given small constraints, it's fine.
Now, produce final answer.
But wait, there is one subtlety: K can be as small as 1. Then mask loop from 1 to 1, okay.
Also, note that N may be up to 500, but K is max 10. So reading lines with .split() might read all tokens. That's fine.
Thus, final solution.
But hold on, reading by splitting may cause issue if there is spaces inside the string? It says string of 0 and 1, no spaces. So safe.
Now, I'll provide final answer.
But the problem might be from some contest, maybe there is a DP solution. But with K≤10, brute force is intended.
Now, I'll produce final solution code.
One more point: The problem statement: "Its for sure that everyone has at least liking for one dish." So each friend has at least one 1. So no friend with all zeros.
Ok.
Now, code:
def solve():
import sys
data = sys.stdin.read().split()
t = int(data[0])
idx = 1
out = []
for _ in range(t):
N = int(data[idx]); K = int(data[idx+1]); idx+=2
likes = []
for i in range(N):
s = data[idx]; idx+=1
# If s length might be different due to missing leading zeros? But it should be K.
mask = 0
# iterate over characters
for j,ch in enumerate(s):
if ch == '1':
mask |= (1 << j)
# In case string length less than K? Not expected but pad?
# However, constraints say each string length K.
likes.append(mask)
min_dishes = K
# Iterate over all non-zero subsets
for mask in range(1, 1<<K):
# optional prune: if bin(mask).count('1') >= min_dishes: continue
ok = True
for like in likes:
if (like & mask) == 0:
ok = False
break
if ok:
cnt = mask.bit_count() if hasattr(int, "bit_count") else bin(mask).count('1')
if cnt < min_dishes:
min_dishes = cnt
if min_dishes == 1: # can't get better
break
out.append(str(min_dishes))
sys.stdout.write("\n".join(out))
if name == "main":
solve()
I used .bit_count() if Python 3.8+? Actually bit_count introduced in Python 3.8? It's in 3.10? Actually Python 3.8 doesn't have, 3.10 has int.bit_count(). To be safe, I'll use bin(mask).count('1') which works everywhere. But we can also check for attribute. But fine.
Now, output.
One more nuance: The data reading: When we use split(), it will split on whitespace. For strings like "10", it's fine. But what if the strings are on the same line separated by spaces? For example, they might be given as "10 01"? The problem says "followed by N lines each of length K". So each on a new line. But even if they are on the same line, split will still give tokens. That's fine because we read exactly N strings after N and K.
But caution: If some line has trailing spaces, split will ignore them. So it's fine.
Now, final answer.
But could there be multiple test cases with large N and K=10? Yes. That's fine.
Thus, solution.
One more improvement: We could precompute for each mask whether it satisfies all friends by checking coverage quickly? Not needed.
Now, produce final output.
Now, I'd like to double-check with sample:
Input:
1
2 2
10
01
Data tokens: ["1","2","2","10","01"]
t=1
N=2, K=2, then likes: s="10": mask: j=0,ch='1' -> set bit0; j=1,ch='0' -> no. So mask=1 (binary 01). Actually wait: bit0 set -> dish1 liked. Then s="01": j=0,ch='0' -> none; j=1,ch='1' -> set bit1 => mask=2 (binary 10). So likes = [1,2]. Now iterate subsets:
mask=1 (01) -> friend1: like&mask=1&1=1 !=0, friend2: 2&1=0 => not ok.
mask=2 (10) -> friend1:1&2=0 -> not ok.
mask=3 (11) -> both ok, cnt=2 -> min=2.
Thus output 2.
Works.
Now, consider scenario where answer 1. For example, 1 1: N=1 K=1, likes: "1". Then mask=1 -> cnt=1 -> min=1.
Thus.
Now, final answer.
I'll produce final code.
But before finalizing, think about if there might be a solution using integer programming? Not needed.
Now, final answer.
I'll output the solution explanation as well. However, the user only asked for answer? The instruction: "You are ChatGPT ... Provide the final answer." So I should output the solution? The user said: "Samu's Birthday is near so she had started planning a party for all of her friends..." It's a problem description. The user likely wants the code or solution. But the instruction says: "You need to find count of minimum dishes to order so that all of her N friends are happy." So it's a programming problem, typical from hackerearth or such. The user likely expects a solution code. In this environment, the answer should be the code. The instruction: "Your task is to provide the final answer, which is the code that solves the problem." So I'll output the Python code.
But also I should include explanation? The instruction: "You are ChatGPT... Provide the final answer." Typically, I give the code. So I'll output the code with some comments.
Thus final answer: Python solution.
One more check: The constraints T ≤ 10, N ≤ 500, K ≤ 10. So my code is efficient.
Potential pitfalls: If K=10, then 1<<K = 1024, fine.
Now, code.
But wait, there might be a nuance: The problem may ask to output -1 if impossible? But they said it's sure that everyone likes at least one dish, so all dishes selected always works. So always possible.
Now, finalize.
But I'm thinking: Could there be an alternative more clever solution? Possibly using bit DP, but not needed.
Thus, final answer.
One more thing: In the code, after reading N and K, we need to read N strings. But what if the strings contain spaces? Not. So fine.
Now, produce final answer.
Let me produce the code.
I'll include the solve function and call it.
Now, final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().split()
if not data:
return
t = int(data[0])
idx = 1
out_lines = []
for _ in range(t):
N = int(data[idx])
K = int(data[idx + 1])
idx += 2
likes = []
for _ in range(N):
s = data[idx]
idx += 1
mask = 0
# bit j (0-indexed) corresponds to dish j+1
for j, ch in enumerate(s):
if ch == '1':
mask |= (1 << j)
likes.append(mask)
min_dishes = K # worst case: take all dishes
# Try all non‑empty subsets of dishes
for subset in range(1, 1 << K):
# optional early prune
# if subset.bit_count() >= min_dishes:
# continue
ok = True
for like in likes:
if (like & subset) == 0:
ok = False
break
if ok:
cnt = bin(subset).count('1') # number of dishes in this subset
if cnt < min_dishes:
min_dishes = cnt
if min_dishes == 1: # cannot improve further
break
out_lines.append(str(min_dishes))
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()