We are asked to solve: given a row of N buildings (0-indexed). Some have bombs (Si=1). When bomb explodes, it destroys its own building and all adjacent buildings (so building i-1 and i+1). All bombs explode simultaneously. We need to count number of buildings that are not destroyed after all explosions.
Given constraints: T up to 100, N up to 1000, so simple simulation per test case is fine.
We need to compute set of destroyed buildings: for each i where Si='1', then i-1, i, i+1 are destroyed (except out of bounds). After processing all bombs (simultaneously), some buildings may be destroyed multiple times but we count unique. Then answer = N - number of destroyed buildings.
Alternatively, we can determine which buildings survive: a building j survives if there is no bomb at j, nor at j-1, nor at j+1. i.e., for all i, if Si='1', then j is not in {i-1,i,i+1}. So essentially we need to count indices j such that S[j]=='0' and (j-1 < 0 or S[j-1]=='0')? Actually careful: condition: no bomb in j-1, j, j+1. So we need S[j]=='0' and (j-1 <0 or S[j-1]=='0') and (j+1>=N or S[j+1]=='0'). But note that bombs are simultaneously, so a bomb at j-1 destroys j, even if j-1 also gets destroyed. So survive if all three positions are 0. However, what if there is bomb at j-1 and also bomb at j? It doesn't matter. So the condition is correct.
But careful: If there is a bomb at j+1, it will also destroy j. So indeed survive if none of j-1, j, j+1 have bomb.
Thus we can just count indices j from 0 to N-1 such that S[j]=='0', and (j==0 or S[j-1]=='0'), and (j==N-1 or S[j+1]=='0').
But is that correct? Let's test with given examples:
Example1: N=3, S=010. j=0: S[0]='0', j+1=1 => S[1]='1' -> fails -> not survive. j=1: S[1]='1' -> fails. j=2: S[2]='0', j-1=1 -> S[1]='1' -> fails. So count=0. matches.
Example2: N=5, S=10001. Let's compute: j0: S0='1' -> fails. j1: S1='0', check j0='1'? fails because j0=1? Actually condition: j-1=0 S0=1, so fails. j2: S2='0', j-1=1 S1='0'? Actually S1='0'? S=1 0 0 0 1? Wait S=10001: indices:0=1,1=0,2=0,3=0,4=1. So j2: S2='0', j-1=1='0' passes, j+1=3='0' passes, so j2 survives? But careful: bombs at 0 and 4. Bomb at 0 destroys buildings 0 and 1. Bomb at 4 destroys buildings 3 and 4. Building 2 is not adjacent to any bomb? Adjacent to 1 and 3. Bomb at 0 destroys building 1, but does it destroy building 2? It destroys building 1 (adjacent) but not building 2 because it's only adjacent to 0 and 1? Actually bomb at building 0 destroys itself and adjacent buildings: building 0 and building 1 only (since i+1=1). It does not destroy building 2. Bomb at building 4 destroys building 4 and building 3. So building 2 remains intact. So answer=1. Our condition yields j=2 qualifies, count=1. Good.
Example3: N=7, all zeros. All survive: count=7.
Thus answer is count of indices j satisfying condition. Alternatively, we can compute destroyed set: iterate bombs and mark indices i-1,i,i+1 if within range, then answer = N - size of destroyed set. Both easy.
Edge cases: overlapping destruction. Condition approach is simpler and O(N) per test.
But careful: Is there any nuance about simultaneous explosion? If a building contains a bomb, it is destroyed regardless. So survive condition is exactly as described.
Thus implement.
But the problem statement says "Find for the Little Elephant the number of buildings that will not be destroyed after all bombs explode." So we output that count.
Constraints small but be careful with input reading.
Implementation:
Read integer T.
For each test case:
Read N (int)
Read string S (may have newline)
Initialize count = 0
For i in 0..N-1:
if S[i]=='1': continue
left_ok = (i==0 or S[i-1]=='0')
right_ok = (i==N-1 or S[i+1]=='0')
if left_ok and right_ok: count+=1
Print count.
That's all.
But double-check: The condition for a building to survive is that there is no bomb in the building itself and no bomb in the immediate neighbors. But what if a bomb is in the building itself? We already skip because S[i]=='1' then we continue. So correct.
Thus solution done.
But let's also think about the situation: If two bombs are adjacent, say building 0 and 1 both have bombs. Bomb at 0 destroys 0 and 1. Bomb at 1 destroys 0,1,2. So building 2 is destroyed due to bomb at 1. So building 2 cannot survive if either neighbor has bomb. So condition holds.
Thus correct.
We can implement in Python.
But the problem is from Codeforces? Possibly test cases up to 1000, T=100, O(N) per test is fine.
We should also consider possible trailing spaces.
We'll write solve() that reads from sys.stdin.
Alternatively, we can use faster input.
Now produce answer in required format: print each answer on its own line.
Edge cases: N=1: if S[0]=='1' then count=0; if '0' then count=1 (since no neighbors). Condition: i=0, left_ok true (i==0), right_ok true (i==N-1), S[0]=='0' => count 1.
Works.
Thus final code.
But the user might want explanation? They said "The problem is" and gave description, maybe they want code. The final answer should be a solution with explanation? The prompt: "Please reason step by step, and put your final answer within \boxed{}." But that's for a different type. Actually the conversation: The user gave the problem statement and expects a solution? Usually they want the answer (code or explanation). Since it's a coding problem, we might output the code? However the instruction says: "Please reason step by step, and put your final answer within \boxed{}." Possibly they want the answer number for given sample? But the sample output is given, they ask for solution. Let's read the user query: "The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy buildings placed in a row and numbered from left to right strating from 0. Each building i (except the first and the last) has exactly two adjacent buildings with indices i-1 and i+1. The first and the last buildings have just a single adjacent building.
Some of the buildings contain bombs. When bomb explodes in some building it destroys it and all adjacent to it buildings.
You are given the string S of length N, where Si is 1 if the i-th building contains bomb, 0 otherwise. Find for the Little Elephant the number of buildings that will not be destroyed after all bombs explode. Please note that all bombs explode simultaneously.
Input
The first line contains single integer T - the number of test cases. T test cases follow. The first line of each test case contains the single integer N - the number of buildings. The next line contains the string S of length N consisted only of digits 0 and 1.
Output
In T lines print T inetgers - the answers for the corresponding test cases.
Constraints
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
Example
Input:
3
3
010
5
10001
7
0000000
Output:
0
1
7"
So they gave the problem statement and ask for answer? The typical thing is to write a program to solve it. But in this environment, we are not executing code. However, the user expects reasoning and final answer? Actually the final answer might be the output for given sample? That doesn't make sense because they already gave output. They likely want the solution algorithm and maybe code.
Given the instruction: "Please reason step by step, and put your final answer within \boxed{}." This is typical for math problems. But here it's a programming problem. Perhaps they want to know the number for each test case? No.
Maybe they want the approach. Or they want the answer to a hidden test? Actually it's ambiguous. Looking at the chat: The user gave the problem description. In many similar prompts, the assistant is supposed to solve the problem and provide the answer (the program). But the specific instruction says to put final answer in \boxed{}; maybe they expect the code as the final answer? But code cannot be inside a box? It could be.
Alternatively, maybe they want the count of survivors for a particular input? But they didn't give specific input besides sample. So I'd assume they want a solution description and code.
Given the "step by step" and "final answer within \boxed{}" pattern, I suspect they want the answer for some specific input? But the problem includes sample input. Actually the sample input is provided to illustrate. Possibly they want to output the answers for that sample? But the output is already shown. So they don't need that.
Maybe this is from a coding challenge and they want the solution code. I think the best approach is to provide a clear explanation of the algorithm and then provide the Python code to solve it. And then put the code in \boxed{}? That would be weird. Actually the instruction likely is from the system prompt for the assistant: "Please reason step by step, and put your final answer within \boxed{}." This is the general instruction for the assistant to output final answer in a box. So I should provide reasoning and then final answer: maybe the final answer is the number for each test case? But there is no specific input.
Given that the problem is to write a program, the final answer could be the program itself. So I will output the code inside \boxed{}? But that seems unusual. Instead, I can output the solution approach and then provide the code block, and then maybe put something like \boxed{\text{Code provided}}? But not.
Let's think: In many Codeforces problems, the solution is to compute something. The answer is a number. So for a given input, the program outputs numbers. However, the user did not give a specific input besides sample. So they want the algorithm.
Given the typical chat format, the user says: "The Little Elephant from the Zoo of Lviv..." They are giving the problem statement and expect you to solve it. This is similar to "Solve this competitive programming problem." The assistant should provide an explanation and then give the code.
I'll follow that: provide reasoning, then present code (Python). And to satisfy the instruction to put final answer in \boxed{}, perhaps I'll put the core formula or answer count for sample? But sample answer already given. Hmm.
Maybe the final answer is the number of surviving buildings for the general case? That's not a number.
Let me re-read: The problem: "Find for the Little Elephant the number of buildings that will not be destroyed after all bombs explode." So the answer is a number per test case. But without input, we can't compute. So it's a problem to be solved by code.
Thus I'll write an explanation and then the code.
But the system's instruction to put final answer in \boxed{} is generic; perhaps they want the answer to the problem for a hidden test? Not.
Maybe they want the solution output for the sample input: 0, 1, 7. But they already gave that. So no.
Given the ambiguous nature, I'll adopt the approach: provide step-by-step reasoning, then provide the Python code that would solve the problem, and then put the output of the sample (which is already known) inside \boxed{}? That seems off.
Better: I'll give the algorithm, and then put the final answer: "For each test case, print the count of indices i where S[i] == '0' and (i == 0 or S[i-1] == '0') and (i == N-1 or S[i+1] == '0')." That's the logic. And then perhaps enclose that statement in a box? But it's not a numeric answer.
Alternatively, I'll just follow the pattern: reasoning, then final answer: The solution is to count the safe buildings using the described condition. Then provide code in a box? Possibly they want the code as final answer. In many similar tasks on this platform, the assistant gives a code solution and puts it in a box.
Let me check previous examples: In many conversations, the assistant provides code inside a code block, and then maybe puts "The solution is as above." but not necessarily box. However the instruction says: "put your final answer within \boxed{}." This is a directive from the system to the assistant to format the final answer. So I should output something like \boxed{0}? That would be weird. I think the system expects a final answer for a mathematical problem; but this is not purely mathematical, it's a programming problem. Maybe the user's query is actually a math problem disguised? Let's parse: They want number of buildings not destroyed. Could be computed by a formula. But it's straightforward.
Given the nature of the platform, I think the assistant's response should include reasoning and then a final answer: maybe the answer for the example? But that would be 0,1,7. But the question doesn't ask "what is the output for the given input?" It's a problem statement from a contest.
Given the ambiguity, I'll provide a clear explanation and then provide Python code that solves it. I'll also mention that for the sample input, the output is 0,1,7.
But to adhere to the instruction, I will put the sample output in \boxed{}? That seems not representative of the solution.
Maybe the user wants the number for a particular test case that they haven't stated? That is unlikely.
Perhaps I misinterpret: The user is giving the problem statement and expects me to "Find for the Little Elephant the number of buildings that will not be destroyed" given that I am to solve it? But no input provided. So maybe they want a generic solution.
Given the typical format of such problems in code runner, the assistant would output code. I'll do that.
Thus final answer: Python code.
I'll put the code inside a \boxed{}? That would be odd because code includes line breaks. Better to put in a code block and then say the final answer is the code? Actually the instruction says "put your final answer within \boxed{}." So I could do \boxed{\text{See code below}} but that's not.
Maybe the instruction is from the system but for this problem, the final answer could be the algorithm in words? Not sure.
I think I will provide the solution and then at the end, put the answer for the sample input (0, 1, 7) inside \boxed{}? But the sample already has output, so that would be redundant.
Maybe the user expects me to compute the answer for a specific hidden test? Not.
Alright, I'll go with a typical response: Explain the approach, then provide code. To satisfy the "final answer within \boxed{}", I will put the code itself inside a box? But code formatting may break. Alternatively, I can put the main formula in a box: \boxed{\text{count} = \sum_{i=0}^{N-1} [S_i = 0 \land (i=0 \lor S_{i-1}=0) \land (i=N-1 \lor S_{i+1}=0)]}. That is a concise mathematical representation. That could be the final answer. Yes! That might be appropriate: the final answer is the expression. So I'll put that in \boxed{}.
I'll produce reasoning step by step and then box the formula.
But then the user might expect a code solution. However the problem is from a coding contest, they might want code. But the instruction to put in a box suggests a math answer. However, I can provide both: explanation and code, and then box the formula.
Given the environment, it's okay.
Thus final answer: the count of indices i such that S[i]=='0' and (i==0 or S[i-1]=='0') and (i==N-1 or S[i+1]=='0').
I will present that in a box.
Now produce the reasoning.
Step by step:
-
When a bomb at building i explodes, it destroys buildings i-1, i, i+1 (within bounds). All bombs explode at the same time, so a building is destroyed if it is within the destruction range of any bomb.
-
Therefore, a building j survives if and only if there is no bomb at j, and no bomb at j-1, and no bomb at j+1. Because if any of those three have a bomb, j will be destroyed.
-
So for each building j, we check S[j] == '0', and (j==0 or S[j-1]=='0'), and (j==N-1 or S[j+1]=='0').
-
Count the number of j satisfying that condition.
-
Output that count for each test case.
That's the algorithm, O(N) per test case, works within constraints.
We can also implement by marking destroyed buildings: create boolean array destroyed initially false, for each i with S[i]=='1', set destroyed[i]=True, if i-1>=0 set destroyed[i-1]=True, if i+1<N set destroyed[i+1]=True, then answer = number of False entries. Both equivalent.
Given N ≤ 1000, T ≤ 100, both fine.
Thus final answer is the formula.
Now write final message: reasoning and then boxed formula.
Optionally, I can include a Python code snippet for reference.
I'll do:
Reasoning...
The number of undestroyed buildings is:
\boxed{ \sum_{i=0}^{N-1} \left[ S_i = 0 \ \land\ (i=0 \ \lor\ S_{i-1}=0) \ \land\ (i=N-1\ \lor\ S_{i+1}=0) \right] }
Or in words: count of indices i where the character is '0' and both neighbors (if they exist) are also '0'.
I'll also provide code.
But to follow the final answer instruction, I'll put the formula in the box.
Thus final answer: \boxed{\text{Count of indices } i \text{ such that } S[i] = 0 \text{ and } (i=0 \text{ or } S[i-1]=0) \text{ and } (i=N-1 \text{ or } S[i+1]=0)}. The buildings that survive are those that are not destroyed by any bomb. A bomb at position (i) destroys buildings (i-1), (i), and (i+1) (within bounds). Therefore, a building (j) is destroyed if there is a bomb at (j-1), (j), or (j+1). Hence, building (j) survives if and only if all three positions are bomb‑free:
- (S[j] = 0)
- (j = 0) or (S[j-1] = 0)
- (j = N-1) or (S[j+1] = 0)
We simply count the number of indices (j) satisfying these conditions.
Complexity: (O(N)) per test case, well within limits.
The answer for each test case is:
[
\boxed{\sum_{j=0}^{N-1} \left[ S_j = 0 ;\land; (j=0 ;\lor; S_{j-1}=0) ;\land; (j=N-1 ;\lor; S_{j+1}=0) \right]}
]
For reference, a Python implementation:
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
idx = 1
out = []
for _ in range(t):
n = int(data[idx]); idx += 1
s = data[idx]; idx += 1
cnt = 0
for i in range(n):
if s[i] == '1':
continue
left_ok = (i == 0) or s[i-1] == '0'
right_ok = (i == n-1) or s[i+1] == '0'
if left_ok and right_ok:
cnt += 1
out.append(str(cnt))
sys.stdout.write("\n".join(out))
if __name__ == "__main__":
solve()