coding-010
A public question in Coding, battery v1. Every answer is shown as the model gave it, as plain text.
30 of 33 ranked models (91%) answered this correctly, from run r20261102-bv1-5246ab. Some of the models below ran in another run and are not in this count.
The question
Prompt
Write a function that counts islands in a grid of `0`s (water) and `1`s (land), where an island is a maximal group of `1`s connected 4-directionally (up/down/left/right only — NOT diagonally). Signature: `def count_islands(grid: list) -> int:` Examples: count_islands([[1,1,0],[0,1,0],[0,0,1]]) -> 2 count_islands([[0,0],[0,0]]) -> 0 Return only one Python code block containing the function. No explanation.
Answers
228 answers; 8 withheld models not counted, best score first, then by model name. A dash means the answer has no definite score, which is different from a score of 0.
Aya 8B 23 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
def dfs(x, y):
if x < 0 or x >= len(grid) or y < 0 or y >= len(grid[0]) or grid[x][y] == 0:
return
grid[x][y] = 0
dfs(x + 1, y)
dfs(x - 1, y)
dfs(x, y + 1)
dfs(x, y - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Aya Expanse 32B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
count = 0
def dfs(i, j):
if i < 0 or j < 0 or i >= rows or j >= cols or grid[i][j] != 1:
return
grid[i][j] = "#"
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Aya Expanse 8B pass score 1.00
Answer (generation ok, stop)
Python solution:
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if (
i < 0
or j < 0
or i >= len(grid)
or j >= len(grid[0])
or grid[i][j] != 1
):
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 7/10.
Codegeex4 9B All pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Codeqwen 7B Chat V1.5 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i-1, j)
dfs(i+1, j)
dfs(i, j-1)
dfs(i, j+1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Codestral 22B V0.1 pass score 1.00
Answer (generation ok, stop)
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
if not grid:
return 0
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Cogito 14B V1 Preview Qwen pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
count = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0:
return
grid[r][c] = 0
dfs(r+1, c)
dfs(r-1, c)
dfs(r, c+1)
dfs(r, c-1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Cogito 8B V1 Preview Llama pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
islands = 0
visited = [[False]*cols for _ in range(rows)]
def dfs(r, c):
if (r < 0 or r >= rows or c < 0 or c >= cols or
grid[r][c] == 0 or visited[r][c]):
return
visited[r][c] = True
dfs(r+1, c)
dfs(r-1, c)
dfs(r, c+1)
dfs(r, c-1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
dfs(i, j)
islands += 1
return islands
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 10/10.
Command R 35B 08 2024 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
count = 0
def dfs(i, j):
if i < 0 or j < 0 or i >= rows or j >= cols or grid[i][j] != 1:
return
grid[i][j] = '#'
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 9/10.
Command R7B (12-2024) pass score 1.00
Answer (generation ok, stop)
Here is the Python solution that solves the problem:
```python
def count_islands(grid: list[list[int]]) -> int:
if not grid:
return 0
def dfs(r, c):
if (
r < 0
or r >= len(grid)
or c < 0
or c >= len(grid[0])
or grid[r][c] == 0
or visited[r][c]
):
return
visited[r][c] = True
dfs(r - 1, c) # Up
dfs(r + 1, c) # Down
dfs(r, c - 1) # Left
dfs(r, c + 1) # Right
count = 0
visited = [[False] * len(grid[0]) for _ in range(len(grid))]
for r in range(len(grid)):
for c in range(len(grid[0])):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 7/10.
Command R7B Arabic 7B 02 2025 pass score 1.00
Answer (generation ok, stop)
Here is the Python solution that solves the problem:
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
def dfs(grid, i, j):
if (
i < 0
or i >= len(grid)
or j < 0
or j >= len(grid[0])
or grid[i][j] == 0
or visited[i][j]
):
return
visited[i][j] = True
dfs(grid, i + 1, j)
dfs(grid, i - 1, j)
dfs(grid, i, j + 1)
dfs(grid, i, j - 1)
visited = [[False] * len(grid[0]) for _ in range(len(grid))]
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1 and not visited[i][j]:
dfs(grid, i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 7/10.
Deepcoder 14B Preview pass score 1.00
Answer (generation ok, stop)
To solve this problem, we need to count the number of islands in a grid where islands are defined as groups of `1`s connected 4-directionally (up, down, left, right). The solution involves using Depth-First Search (DFS) to explore each island and mark all its connected cells as visited to avoid counting them again.
### Approach
1. **Initialization**: Start by checking if the grid is empty. If it is, return 0 as there are no islands.
2. **Iterate through each cell**: Loop through each cell in the grid. When a cell with a value of `1` is found, it indicates the start of a new island.
3. **DFS Exploration**: For each new island found, use DFS to explore all connected cells. During the DFS, mark each visited cell by setting its value to `0` to avoid revisiting it.
4. **Count Islands**: Each time a new island is found (i.e., a cell with value `1` is encountered), increment the island count and initiate the DFS to mark all connected cells as visited.
### Solution Code
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows = len(grid)
cols = len(grid[0])
count = 0
def dfs(i, j):
if i < 0 or i >= rows or j < 0 or j >= cols:
return
if grid[i][j] != 1:
return
grid[i][j] = 0 # mark as visited
dfs(i - 1, j)
dfs(i + 1, j)
dfs(i, j - 1)
dfs(i, j + 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
```
### Explanation
- **Initialization**: The function starts by checking if the grid is empty. If it is, the function immediately returns 0.
- **DFS Function**: The nested `dfs` function is defined to recursively explore all cells connected to the current cell. It marks each visited cell by setting its value to `0` to avoid revisiting.
- **Main Loop**: The outer loop iterates over each cell in the grid. When a cell with a value of `1` is found,
Cut here for length. The model page has the full answer.
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 7/10.
Deepseek Coder 1.3B Instruct pass score 1.00
Answer (generation ok, stop)
Here is a Python code block that defines the function:
```python
def count_islands(grid):
def dfs(i, j):
if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(i-1, j)
dfs(i+1, j)
dfs(i, j-1)
dfs(i, j+1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
This function uses Depth-First Search (DFS) to traverse the grid and mark all connected `1`s as `0`s. It starts from each `1` and explores all its 4-directional neighbors. When it reaches the end of the grid, it increments the count.
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 7/10.
Deepseek Coder 33B Instruct pass score 1.00
Answer (generation ok, stop)
def count_islands(grid: list) -> int:
if not grid:
return 0
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(grid, i, j)
count += 1
return count
def dfs(grid, i, j):
if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = '#'
dfs(grid, i+1, j)
dfs(grid, i-1, j)
dfs(grid, i, j+1)
dfs(grid, i, j-1)
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 7/10.
Deepseek Coder 6.7B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
def dfs(i, j):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(i-1, j)
dfs(i+1, j)
dfs(i, j-1)
dfs(i, j+1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Deepseek Llm 7B Chat pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
def dfs(i, j):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i+1, j)
dfs(i-1, j)
dfs(i, j+1)
dfs(i, j-1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Deepseek R1 8B Llama Distill pass score 1.00
Answer (generation ok, stop)
To solve this problem, we need to count the number of islands in a grid where an island is defined as a maximal group of `1`s (land) connected 4-directionally (up, down, left, right). The solution involves using a breadth-first search (BFS) approach to explore and mark all connected `1`s starting from each unvisited `1` cell.
### Approach
1. **Check for Empty Grid**: If the grid is empty, return 0 as there are no islands.
2. **Initialize Variables**: Determine the number of rows and columns in the grid. If there are no rows or columns, return 0.
3. **Create Visited Matrix**: Use a 2D list to keep track of visited cells to avoid counting the same cell multiple times.
4. **Iterate Through Each Cell**: For each cell in the grid, if it contains a `1` and has not been visited, it indicates the start of a new island.
5. **BFS Exploration**: From the starting cell, use a queue to explore all 4-directionally connected `1`s, marking them as visited.
6. **Count Islands**: Increment the island count each time a new island is found.
### Solution Code
```python
from collections import deque
def count_islands(grid: list) -> int:
if not grid:
return 0
rows = len(grid)
cols = len(grid[0]) if rows > 0 else 0
if cols == 0:
return 0
visited = [[False for _ in range(cols)] for _ in range(rows)]
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
count += 1
queue = deque()
queue.append((i, j))
visited[i][j] = True
while queue:
x, y = queue.popleft()
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
nx = x + dx
ny = y + dy
if 0 <= nx < rows and 0 <= ny < cols:
if grid[nx][ny] == 1 and not visited[nx][ny]:
visited[nx][ny] = True
Cut here for length. The model page has the full answer.
Judge scores: anthropic/claude-haiku-5.5 6/10; openai/gpt-5.6-luna 7/10.
Deepseek V2 16B Lite Chat pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i: int, j: int):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i+1, j)
dfs(i-1, j)
dfs(i, j+1)
dfs(i, j-1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
DeepSeek-Coder-V2-Lite-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = set()
def dfs(r, c):
if (r < 0 or c < 0 or r >= rows or c >= cols or grid[r][c] == 0 or (r, c) in visited):
return
visited.add((r, c))
directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for dr, dc in directions:
dfs(r + dr, c + dc)
islands = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and (r, c) not in visited:
islands += 1
dfs(r, c)
return islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 10/10.
DeepSeek-R1-0528-Qwen3-8B pass score 1.00
Answer (generation ok, stop)
To solve this problem, we need to count the number of islands in a grid where islands are defined as maximal groups of '1's connected 4-directionally (up, down, left, right). The solution involves traversing the grid and using a depth-first search (DFS) approach to mark all connected '1's as visited, thereby counting each distinct group only once.
### Approach
The approach involves iterating through each cell in the grid. Whenever a '1' is encountered that hasn't been visited, we initiate a DFS to explore all connected '1's starting from that cell. During the DFS, we mark each visited cell to avoid reprocessing. The number of times we start a DFS corresponds to the number of islands in the grid.
### Solution Code
```python
def count_islands(grid):
if not grid:
return 0
rows = len(grid)
cols = len(grid[0])
visited = [[False] * cols for _ in range(rows)]
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
count += 1
stack = [(i, j)]
visited[i][j] = True
while stack:
x, y = stack.pop()
for dx, dy in [(1,0), (-1,0), (0,1), (0,-1)]:
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols and grid[nx][ny] == 1 and not visited[nx][ny]:
visited[nx][ny] = True
stack.append((nx, ny))
return count
```
### Explanation
1. **Initialization**: The function starts by checking if the grid is empty. If it is, the function returns 0 since there are no islands.
2. **Grid Dimensions**: The dimensions of the grid (rows and columns) are determined to set up the visited matrix.
3. **Visited Matrix**: A matrix of the same dimensions as the grid is created to keep track of visited cells, initialized with False.
4. **Iteration and DFS**: The code iterates through each cell in the g
Cut here for length. The model page has the full answer.
Judge scores: anthropic/claude-haiku-5.5 6/10; openai/gpt-5.6-luna 7/10.
DeepSeek-R1-Distill-Qwen-14B pass score 1.00
Answer (generation ok, stop)
To solve this problem, we need to count the number of islands in a grid where each cell is either water (0) or land (1). An island is defined as a maximal group of 1s connected 4-directionally (up, down, left, right).
### Approach
The approach to solve this problem involves using Depth-First Search (DFS) to explore each island. The steps are as follows:
1. **Iterate through each cell in the grid**: For each cell, if it contains a 1, it indicates the start of a new island.
2. **DFS Exploration**: When a new island is found, use DFS to mark all connected 1s as visited. This is done by converting visited 1s to 0s to avoid revisiting them.
3. **Count Islands**: Each time a new island is encountered, increment the count.
The DFS function will recursively check all four possible directions (up, down, left, right) from the current cell. If any of these directions contain a 1, it will continue the DFS from that cell, effectively marking all connected 1s as part of the same island.
### Solution Code
```python
def count_islands(grid: list) -> int:
rows = len(grid)
if rows == 0:
return 0
cols = len(grid[0])
count = 0
def dfs(i, j):
if i < 0 or i >= rows or j < 0 or j >= cols:
return
if grid[i][j] != 1:
return
grid[i][j] = 0 # mark as visited
dfs(i - 1, j) # up
dfs(i + 1, j) # down
dfs(i, j - 1) # left
dfs(i, j + 1) # right
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
```
### Explanation
- **Initialization**: The function starts by determining the number of rows and columns in the grid. If the grid is empty, it returns 0.
- **DFS Helper Function**: The nested `dfs` function marks the current cell as visited by setting it to 0 and recursively explores all four possible directions.
- **Main Loop**: The outer loop iterates through each cell in th
Cut here for length. The model page has the full answer.
Judge scores: anthropic/claude-haiku-5.5 6/10; openai/gpt-5.6-luna 7/10.
DeepSeek-R1-Distill-Qwen-7B pass score 1.00
Answer (generation ok, stop)
To solve this problem, we need to count the number of islands in a grid where an island is defined as a maximal group of `1`s connected 4-directionally (up, down, left, right). The approach involves using a Breadth-First Search (BFS) algorithm to explore each island starting from each unvisited `1` cell.
### Approach
1. **Iterate through each cell**: Traverse each cell in the grid. If a cell contains a `1`, it indicates the start of a new island.
2. **BFS for island exploration**: For each unvisited `1` cell, initiate a BFS to explore all connected `1` cells. During this exploration, mark each visited cell by setting it to `0` to avoid revisiting.
3. **Count islands**: Each time we start a BFS from a new `1` cell, we increment the island count by 1.
### Solution Code
```python
import collections
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
count += 1
# BFS to mark all connected 1s as visited
queue = collections.deque()
queue.append((i, j))
grid[i][j] = 0 # mark as visited
while queue:
x, y = queue.popleft()
# Check all four directions
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
nx = x + dx
ny = y + dy
if 0 <= nx < rows and 0 <= ny < cols and grid[nx][ny] == 1:
queue.append((nx, ny))
grid[nx][ny] = 0 # mark as visited
return count
```
### Explanation
- **Initialization**: Check if the grid is empty. If it is, return 0 as there are no islands.
- **Grid Traversal**: Loop through each cell in the grid. For each cell with a `1`, increment the island count and start a BFS.
- **BFS Implement
Cut here for length. The model page has the full answer.
Judge scores: anthropic/claude-haiku-5.5 6/10; openai/gpt-5.6-luna 7/10.
Devstral 24B Small 2505 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0 or visited[r][c]:
return
visited[r][c] = True
for dr, dc in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
dfs(r + dr, c + dc)
island_count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
island_count += 1
return island_count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Devstral Small 2 24B Instruct (2512) pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows = len(grid)
cols = len(grid[0])
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
count += 1
stack = [(i, j)]
grid[i][j] = 0
while stack:
x, y = stack.pop()
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols and grid[nx][ny] == 1:
grid[nx][ny] = 0
stack.append((nx, ny))
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
This answer on the page of Devstral Small 2 24B Instruct (2512)
dolphin-2.9.1-yi-1.5-34b pass score 1.00
Answer (generation ok, stop)
def count_islands(grid):
def dfs(grid, i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(grid, i + 1, j)
dfs(grid, i - 1, j)
dfs(grid, i, j + 1)
dfs(grid, i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(grid, i, j)
count += 1
return count
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Exaone Deep 2.4B pass score 1.00
Answer (generation ok, stop)
<thought> Okay, I need to write a function called count_islands that takes a grid and returns the number of islands. Hmm, islands are groups of 1s connected horizontally or vertically. So the problem is similar to the classic number of islands problem. Let me think about how to approach this. First, the standard approach for such grid problems is to use either BFS or DFS to explore each island once it's found. The idea is to iterate through each cell in the grid. When a cell with value 1 is found that hasn't been visited yet, we increment the count and then mark all the connected 1s as visited (or change them to 0 to avoid using extra space). Wait, right. To avoid using a separate visited structure, we can modify the grid in place. So whenever we find a 1, we start a DFS or BFS to turn all connected 1s into 0s, effectively marking them as part of the current island. That way, we don't process them again. So the steps would be: 1. Iterate over each cell in the grid. 2. For each cell, if it's a 1, increment the count and then perform DFS/BFS to mark all connected land as visited (by setting them to 0). 3. Continue until all cells are processed. Now, which method is better here? Since the grid can be modified, either approach works. Let's think about the code structure. The function will loop through each row and column. For each cell (i,j), if grid[i][j] is 1, then we need to trigger a search. Let's pick DFS first. But recursion depth could be an issue for very large grids, but maybe the problem expects the standard solution. Alternatively, BFS using a queue might be better to avoid stack overflow for large islands. Hmm, but in Python, recursion depth is limited. For example, if the grid is 10^4 x 10^4, recursion would hit maximum depth. But given that the problem examples are small, perhaps the intended solution is the standard DFS approach. Alternatively, maybe the problem expects an iterative approach. Let's think of writing the DFS iteratively to avoid sta
Cut here for length. The model page has the full answer.
Judge scores: anthropic/claude-haiku-5.5 4/10; openai/gpt-5.6-luna 7/10.
Exaone Deep 7.8B pass score 1.00
Answer (generation ok, stop)
<thought> Okay, I need to write a function called count_islands that takes a grid and returns the number of islands. An island is a group of 1's connected horizontally or vertically. So, the problem is similar to the classic number of islands problem. Let me think about how to approach this. First, the grid is a list of lists. Each element is either 0 or 1. The islands are formed by adjacent 1's (up, down, left, right). So the standard approach here is to use either BFS or DFS to explore each island once and mark visited cells to avoid counting them again. Hmm, right. So the steps would be: 1. Iterate through each cell in the grid. 2. When a cell with value 1 is found that hasn't been visited yet, it's the start of a new island. 3. Perform a BFS or DFS to mark all connected 1's as visited. 4. Increment the island count each time this happens. But how do I track visited cells? Since modifying the input grid is allowed (assuming that's acceptable), I can just change the 1's to 0's once they are visited. That way, the next time we encounter a 1, it's part of a new island. This avoids using extra space for a visited matrix. Yes, that's a common optimization. So the plan is: Loop over each row and column. For each cell (i,j): - If grid[i][j] is 1, then we found a new island. Increment count. - Then perform BFS/DFS to mark all connected 1's as 0's. So for the BFS approach, when we find a 1, we add it to a queue, then for each element in the queue, we check all four directions. For each direction, if it's within bounds and is 1, we set it to 0 and add to the queue. This way, the entire island is marked as visited (by turning to 0) and won't be counted again. Alternatively, using DFS recursively. But for large grids, recursion depth might be an issue. But given that the problem constraints probably allow it, either approach is okay. Let me think about the code structure. The function will take the grid as input. But since lists are mutable, modifying the grid in p
Cut here for length. The model page has the full answer.
Judge scores: anthropic/claude-haiku-5.5 6/10; openai/gpt-5.6-luna 7/10.
EXAONE-3.5-7.8B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0 or visited[r][c]:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
island_count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
island_count += 1
return island_count
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 9/10.
Exaone3.5 2.4B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
count = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0:
return
grid[r][c] = 0 # Mark as visited
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
count += 1
dfs(r, c)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Exaone3.5 32B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = set()
def dfs(r, c):
if (r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0 or (r, c) in visited):
return
visited.add((r, c))
directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for dr, dc in directions:
dfs(r + dr, c + dc)
island_count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and (r, c) not in visited:
dfs(r, c)
island_count += 1
return island_count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Falcon3 10B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] == 1:
grid[i][j] = 0
dfs(i+1, j)
dfs(i-1, j)
dfs(i, j+1)
dfs(i, j-1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Falcon3 3B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Falcon3 7B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Flint 40B A2B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
count = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols:
return
if visited[r][c] or grid[r][c] == 0:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
count += 1
dfs(r, c)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Gemma 3n E4B IT pass score 1.00
Answer (generation ok, stop)
```python def count_islands(grid: list) -> int: ▁▁▁▁if not grid: ▁▁▁▁▁▁▁▁return 0 ▁▁▁▁rows, cols = len(grid), len(grid[0]) ▁▁▁▁num_islands = 0 ▁▁▁▁def dfs(row, col): ▁▁▁▁▁▁▁▁if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == 0: ▁▁▁▁▁▁▁▁▁▁▁▁return ▁▁▁▁▁▁▁▁grid[row][col] = 0▁▁# Mark as visited (water) ▁▁▁▁▁▁▁▁dfs(row + 1, col) ▁▁▁▁▁▁▁▁dfs(row - 1, col) ▁▁▁▁▁▁▁▁dfs(row, col + 1) ▁▁▁▁▁▁▁▁dfs(row, col - 1) ▁▁▁▁for i in range(rows): ▁▁▁▁▁▁▁▁for j in range(cols): ▁▁▁▁▁▁▁▁▁▁▁▁if grid[i][j] == 1: ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁num_islands += 1 ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁dfs(i, j) ▁▁▁▁return num_islands ```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Gemma 4 12B IT pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
visited = set()
count = 0
def dfs(r, c):
# Check boundaries, if it's water, or if already visited
if (r < 0 or r >= rows or
c < 0 or c >= cols or
grid[r][c] == 0 or
(r, c) in visited):
return
visited.add((r, c))
# Explore 4-directional neighbors
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and (r, c) not in visited:
count += 1
dfs(r, c)
return count
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 9/10.
Gemma 7B Instruct V1.1 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
count = 0
for row in range(len(grid)):
for col in range(len(grid[0])):
if grid[row][col] == 1:
count += 1
dfs(grid, row, col)
return count
def dfs(grid, row, col):
grid[row][col] = 0
for row_delta, col_delta in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
new_row, new_col = row + row_delta, col + col_delta
if 0 <= new_row < len(grid) and 0 <= new_col < len(grid[0]) and grid[new_row][new_col] == 1:
dfs(grid, new_row, new_col)
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
gemma-2-9b-it pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
num_islands = 0
def dfs(row, col):
if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == 0:
return
grid[row][col] = 0
dfs(row + 1, col)
dfs(row - 1, col)
dfs(row, col + 1)
dfs(row, col - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
num_islands += 1
dfs(i, j)
return num_islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
gemma-3-12b-it pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
num_islands = 0
def dfs(row, col):
if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == 0:
return
grid[row][col] = 0 # Mark as visited
# Explore adjacent cells
dfs(row + 1, col)
dfs(row - 1, col)
dfs(row, col + 1)
dfs(row, col - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
num_islands += 1
dfs(i, j)
return num_islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
gemma-3-27b-it pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
num_islands = 0
def dfs(row, col):
if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == 0:
return
grid[row][col] = 0 # Mark as visited (sink the island)
# Explore adjacent cells
dfs(row + 1, col)
dfs(row - 1, col)
dfs(row, col + 1)
dfs(row, col - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
num_islands += 1
dfs(i, j)
return num_islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
gemma-3-4b-it pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
"""
Counts islands in a grid of 0s (water) and 1s (land), where an island is a maximal group of 1s connected
4-directionally (up/down/left/right only — NOT diagonally).
"""
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
num_islands = 0
def dfs(row, col):
if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == 0:
return
grid[row][col] = 0 # Mark as visited
dfs(row + 1, col)
dfs(row - 1, col)
dfs(row, col + 1)
dfs(row, col - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
num_islands += 1
dfs(i, j)
return num_islands
```
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 9/10.
gemma-4-26B-A4B-it pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows = len(grid)
cols = len(grid[0])
visited = set()
island_count = 0
def dfs(r, c):
if (r < 0 or r >= rows or
c < 0 or c >= cols or
grid[r][c] == 0 or
(r, c) in visited):
return
visited.add((r, c))
# Explore 4-directional neighbors
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and (r, c) not in visited:
island_count += 1
dfs(r, c)
return island_count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
gemma-4-31B-it pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = set()
count = 0
def dfs(r, c):
if (r < 0 or r >= rows or c < 0 or c >= cols or
grid[r][c] == 0 or (r, c) in visited):
return
visited.add((r, c))
# Check 4-directional neighbors
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and (r, c) not in visited:
count += 1
dfs(r, c)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 10/10.
Gemma2 27B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
num_islands = 0
def dfs(row, col):
if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == 0:
return
grid[row][col] = 0
dfs(row + 1, col)
dfs(row - 1, col)
dfs(row, col + 1)
dfs(row, col - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
num_islands += 1
dfs(i, j)
return num_islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Gemma2 2B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
num_islands = 0
def dfs(row, col):
if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == 0:
return
grid[row][col] = 0 # Mark the current cell as visited
dfs(row + 1, col)
dfs(row - 1, col)
dfs(row, col + 1)
dfs(row, col - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
num_islands += 1
dfs(i, j)
return num_islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Gemma3 1B IT pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
"""
Counts the number of islands in a grid of 0s and 1s.
An island is a maximal group of 1s connected 4-directionally (up/down/left/right).
Args:
grid: A list of lists representing the grid.
Returns:
The number of islands in the grid.
"""
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
num_islands = 0
def dfs(row, col):
if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == 0:
return
grid[row][col] = 0 # Mark as visited
dfs(row + 1, col)
dfs(row - 1, col)
dfs(row, col + 1)
dfs(row, col - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
num_islands += 1
dfs(i, j)
return num_islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Gemma3n E2B IT pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
num_islands = 0
def dfs(row, col):
if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == 0:
return
grid[row][col] = 0 # Mark as visited
dfs(row + 1, col)
dfs(row - 1, col)
dfs(row, col + 1)
dfs(row, col - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
num_islands += 1
dfs(i, j)
return num_islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Gemma4 E2B IT pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
num_islands = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0:
return
# Mark as visited by sinking the land (changing 1 to 0)
grid[r][c] = 0
# Explore neighbors
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
# Create a mutable copy if the input grid should not be modified,
# but for this problem structure, modifying the input grid is a common approach.
# If modification is forbidden, use a separate 'visited' set.
# Assuming modification of the input grid is acceptable for simplicity:
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
num_islands += 1
dfs(r, c)
return num_islands
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 9/10.
Gemma4 E4B IT pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
"""
Counts the number of islands in a 2D grid of 0s (water) and 1s (land).
An island is a maximal group of 1s connected 4-directionally.
"""
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
island_count = 0
def dfs(r, c):
# Check boundaries and if the cell is water or already visited (marked as 0)
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0:
return
# Mark the current land cell as visited by turning it into water (0)
grid[r][c] = 0
# Explore neighbors (up, down, left, right)
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
# Iterate through every cell in the grid
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
# Found the start of a new island
island_count += 1
# Use DFS to sink (mark as visited) the entire island
dfs(r, c)
return island_count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
GLM-4.5-Air pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
"""
Counts the number of islands in a 2D grid.
An island is a group of '1's (land) connected 4-directionally (up, down, left, right).
The grid is represented as a list of lists of integers (0 for water, 1 for land).
Args:
grid: A 2D list of integers representing the map.
Returns:
The total number of islands found in the grid.
"""
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
island_count = 0
def dfs(r, c):
"""
Depth-First Search to mark all connected land as visited.
It changes the value of the grid cell from 1 to 0 to mark it as visited.
"""
# Base case: if out of bounds or the cell is water, stop the search.
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0:
return
# Mark the current cell as visited by setting it to water.
grid[r][c] = 0
# Recursively visit all 4-directionally adjacent land cells.
dfs(r + 1, c) # Down
dfs(r - 1, c) # Up
dfs(r, c + 1) # Right
dfs(r, c - 1) # Left
# Iterate through every cell in the grid.
for r in range(rows):
for c in range(cols):
# If we find a land cell, it's the start of a new island.
if grid[r][c] == 1:
island_count += 1
# Perform DFS to mark all connected land as visited.
dfs(r, c)
return island_count
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 9/10.
GLM-4.7-Flash pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows = len(grid)
cols = len(grid[0])
count = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] != 1:
return
grid[r][c] = 0
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Glm4 9B Chat pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
gpt-oss-120b pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r: int, c: int) -> None:
stack = [(r, c)]
while stack:
x, y = stack.pop()
if visited[x][y]:
continue
visited[x][y] = True
for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols:
if grid[nx][ny] == 1 and not visited[nx][ny]:
stack.append((nx, ny))
islands = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
islands += 1
dfs(i, j)
return islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; deepseek/deepseek-v4.1-flash 9/10.
gpt-oss-20b pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
stack = [(r, c)]
while stack:
x, y = stack.pop()
if visited[x][y]:
continue
visited[x][y] = True
for dx, dy in ((1,0),(-1,0),(0,1),(0,-1)):
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols and not visited[nx][ny] and grid[nx][ny] == 1:
stack.append((nx, ny))
islands = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
dfs(i, j)
islands += 1
return islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; deepseek/deepseek-v4.1-flash 9/10.
Granite Code 8B Instruct pass score 1.00
Answer (generation ok, stop)
Here's how you can implement this:
```python
def count_islands(grid):
if not grid or not grid[0]:
return 0
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 7/10.
granite-4.1-3b pass score 1.00
Answer (generation ok, stop)
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = set()
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
def dfs(r, c):
if (r, c) in visited or r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0:
return
visited.add((r, c))
for dr, dc in directions:
dfs(r + dr, c + dc)
island_count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and (r, c) not in visited:
dfs(r, c)
island_count += 1
return island_count
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Granite3 Dense 2B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
islands = 0
def dfs(i, j):
if i < 0 or i >= rows or j < 0 or j >= cols or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i-1, j)
dfs(i+1, j)
dfs(i, j-1)
dfs(i, j+1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
dfs(i, j)
islands += 1
return islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Granite3 Dense 8B Instruct pass score 1.00
Answer (generation ok, stop)
Here is the Python function that counts the number of islands in a given grid:
```python
def count_islands(grid):
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += 1
dfs(grid, i, j)
return count
def dfs(grid, i, j):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(grid, i+1, j)
dfs(grid, i-1, j)
dfs(grid, i, j+1)
dfs(grid, i, j-1)
```
This function uses Depth-First Search (DFS) to traverse the grid and mark visited cells as `0`. The `count_islands` function iterates through the grid and calls the `dfs` function for each cell containing `1`. The `dfs` function marks the current cell as visited and recursively calls itself for the neighboring cells.
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 7/10.
Granite3 Moe 3B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Granite3.1 Dense 2B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
if r < 0 or c < 0 or r >= rows or c >= cols or grid[r][c] != 1 or visited[r][c]:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
island_count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
island_count += 1
return island_count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Granite3.1 Dense 8B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
if not grid:
return 0
def dfs(i, j):
if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0 # Mark as visited
dfs(i+1, j) # Down
dfs(i-1, j) # Up
dfs(i, j+1) # Right
dfs(i, j-1) # Left
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 9/10.
Granite3.1 Moe 1B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
def dfs(x, y, visited, grid):
if x < 0 or y < 0 or x >= len(grid) or y >= len(grid[0]) or grid[x][y] == 0 or visited[x][y]:
return
visited[x][y] = True
dfs(x + 1, y, visited, grid)
dfs(x - 1, y, visited, grid)
dfs(x, y + 1, visited, grid)
dfs(x, y - 1, visited, grid)
visited = [[False for _ in range(len(grid[0]))] for _ in range(len(grid))]
islands = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1 and not visited[i][j]:
dfs(i, j, visited, grid)
islands += 1
return islands
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 7/10.
Granite3.1 Moe 3B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
def dfs(i, j):
if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0 # Mark the cell as visited
dfs(i+1, j)
dfs(i-1, j)
dfs(i, j+1)
dfs(i, j-1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Granite3.2 2B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
if r < 0 or c < 0 or r >= rows or c >= cols or grid[r][c] != 1 or visited[r][c]:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
island_count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
island_count += 1
return island_count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Granite3.2 8B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
if not grid:
return 0
def dfs(i, j):
if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0 # Mark as visited
dfs(i+1, j) # Down
dfs(i-1, j) # Up
dfs(i, j+1) # Right
dfs(i, j-1) # Left
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 9/10.
Granite3.3 2B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
if not grid:
return 0
def dfs(i, j):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Granite3.3 8B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
def dfs(i, j):
if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i+1, j)
dfs(i-1, j)
dfs(i, j+1)
dfs(i, j-1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Granite4 1B H pass score 1.00
Answer (generation ok, stop)
def count_islands(grid):
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Granite4 350M H pass score 1.00
Answer (generation ok, stop)
def count_islands(grid):
islands = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
islands += 1
dfs(grid, i, j)
return islands
def dfs(grid, i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(grid, i + 1, j)
dfs(grid, i - 1, j)
dfs(grid, i, j + 1)
dfs(grid, i, j - 1)
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Granite4 3B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
count = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0:
return
grid[r][c] = 0 # mark as visited
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
count += 1
dfs(r, c)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Granite4 Micro H pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i: int, j: int):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(i+1, j)
dfs(i-1, j)
dfs(i, j+1)
dfs(i, j-1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 8/10.
Granite4 Small H pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
if not grid:
return 0
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Granite4 Tiny H pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Granite4.1 30B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
island_count = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] != 1:
return
grid[r][c] = -1 # Mark as visited
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
island_count += 1
dfs(r, c)
return island_count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Granite4.1 8B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
if not grid:
return 0
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Granite4.2 30B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
count = 0
def dfs(r, c):
if (r < 0 or r >= rows or c < 0 or c >= cols or
visited[r][c] or grid[r][c] == 0):
return
visited[r][c] = True
dfs(r - 1, c)
dfs(r + 1, c)
dfs(r, c - 1)
dfs(r, c + 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
count += 1
dfs(r, c)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 10/10.
Granite4.2 3B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0 or visited[r][c]:
return
visited[r][c] = True
for dr, dc in directions:
dfs(r + dr, c + dc)
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Granite4.2 8B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or visited[r][c] or grid[r][c] == 0:
return
visited[r][c] = True
dfs(r-1, c)
dfs(r+1, c)
dfs(r, c-1)
dfs(r, c+1)
count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Hermes-3-Llama-3.1-8B pass score 1.00
Answer (generation ok, stop)
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i-1, j)
dfs(i+1, j)
dfs(i, j-1)
dfs(i, j+1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Internlm2 1.8B Chat V2.5 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
def dfs(grid, i, j):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(grid, i + 1, j)
dfs(grid, i - 1, j)
dfs(grid, i, j + 1)
dfs(grid, i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(grid, i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Internlm2 7B Chat V2.5 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
def dfs(grid, i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(grid, i + 1, j)
dfs(grid, i - 1, j)
dfs(grid, i, j + 1)
dfs(grid, i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(grid, i, j)
count += 1
return count
```[UNUSED_TOKEN_145]
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Laguna XS 2.1 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows = len(grid)
cols = len(grid[0])
count = 0
def dfs(i, j):
if i < 0 or i >= rows or j < 0 or j >= cols or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 9/10.
Lfm2 24B pass score 1.00
Answer (generation ok, stop)
```
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0:
return
grid[r][c] = 0
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Lfm2.5 8B A1B pass score 1.00
Answer (generation ok, stop)
<think>
We need to write a function count_islands that counts islands in a grid of 0s and 1s, with islands defined as maximal group of 1s connected 4-directionally.
The problem is straightforward: perform DFS or BFS to find all connected components of 1s. Return number of such components.
We need to produce only one Python code block containing the function. No explanation. So we output something like:
```python
def count_islands(grid):
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False for _ in range(cols)] for _ in range(rows)]
def dfs(r, c):
stack = [(r,c)]
visited[r][c] = True
while stack:
x,y = stack.pop()
for dx,dy in ((1,0),(-1,0),(0,1),(0,-1)):
nx, ny = x+dx, y+dy
if 0 <= nx < rows and 0 <= ny < cols and grid[nx][ny]==1 and not visited[nx][ny]:
visited[nx][ny] = True
stack.append((nx,ny))
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j]==1 and not visited[i][j]:
dfs(i,j)
count += 1
return count
```
That's fine. Could also use BFS with deque, but DFS is okay.
Make sure to handle empty grid: if not grid or len(grid)==0 returns 0. Also ensure that we treat grid as list of lists; each row length may vary? Usually rectangular. We'll assume uniform width.
Thus final answer: a code block with the function.
We must output only one Python code block containing the function, no extra text. So:
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False for _ in range(cols)] for _ in range(rows)]
def dfs(r, c):
stack = [(r, c)]
visited[r][c] = True
while stack:
x, y = stack.pop()
for dx, dy in ((1,0), (-1,0), (0,1), (0,-1)):
nx, ny = x + dx, y + dy
Cut here for length. The model page has the full answer.
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 7/10.
Lfm2.5 Thinking 1.2B pass score 1.00
Answer (generation ok, stop)
```python
from collections import deque
def count_islands(grid: list) -> int:
count = 0
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 1:
count += 1
queue = deque()
queue.append((i, j))
grid[i][j] = 0
while queue:
x, y = queue.popleft()
for dx, dy in [(-1,0),(1,0),(0,-1),(0,1)]:
nx, ny = x + dx, y + dy
if 0 <= nx < len(grid) and 0 <= ny < len(grid[0]) and grid[nx][ny] == 1:
grid[nx][ny] = 0
queue.append((nx, ny))
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Lfm3 1.7B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
if not grid:
return 0
count = 0
rows = len(grid)
cols = len(grid[0]) if rows > 0 else 0
def dfs(r, c):
stack = [(r, c)]
while stack:
x, y = stack.pop()
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols and grid[nx][ny] == 1:
stack.append((nx, ny))
grid[nx][ny] = 0 # mark visited
return
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
count += 1
dfs(i, j)
grid[i][j] = 0 # mark as visited after DFS
return count
```
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 9/10.
Llama 4 Scout 17B-16E Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
count = 0
rows, cols = len(grid), len(grid[0])
def dfs(r, c):
if 0 <= r < rows and 0 <= c < cols and grid[r][c] == 1:
grid[r][c] = 0
dfs(r - 1, c)
dfs(r + 1, c)
dfs(r, c - 1)
dfs(r, c + 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Llama-3.1-8B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
islands = 0
def dfs(r, c):
if 0 <= r < rows and 0 <= c < cols and grid[r][c] == 1:
grid[r][c] = 0
dfs(r-1, c)
dfs(r+1, c)
dfs(r, c-1)
dfs(r, c+1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
dfs(r, c)
islands += 1
return islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Llama-3.2-3B-Instruct pass score 1.00
Answer (generation ok, stop)
def count_islands(grid: list) -> int:
if not grid:
return 0
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(grid, i, j)
count += 1
return count
def dfs(grid: list, i: int, j: int):
if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(grid, i+1, j)
dfs(grid, i-1, j)
dfs(grid, i, j+1)
dfs(grid, i, j-1)
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Llama-3.3-70B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
"""
Counts the number of islands in a grid of 0s (water) and 1s (land).
Args:
grid (list): A 2D list of 0s and 1s representing water and land respectively.
Returns:
int: The number of islands in the grid.
"""
if not grid:
return 0
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(grid, i, j)
count += 1
return count
def dfs(grid: list, i: int, j: int) -> None:
"""
Performs a depth-first search on the grid starting from the given position.
Args:
grid (list): A 2D list of 0s and 1s representing water and land respectively.
i (int): The row index of the current position.
j (int): The column index of the current position.
"""
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(grid, i - 1, j)
dfs(grid, i + 1, j)
dfs(grid, i, j - 1)
dfs(grid, i, j + 1)
# Example usage:
print(count_islands([[1, 1, 0], [0, 1, 0], [0, 0, 1]])) # Output: 2
print(count_islands([[0, 0], [0, 0]])) # Output: 0
```
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 7/10.
Llama3.2 1B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
if not grid:
return 0
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(grid, i, j)
count += 1
return count
def dfs(grid, i, j):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(grid, i - 1, j)
dfs(grid, i + 1, j)
dfs(grid, i, j - 1)
dfs(grid, i, j + 1)
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 7/10.
Marco O1 7B pass score 1.00
Answer (generation ok, stop)
<Thought> Alright, I need to write a function that counts the number of islands in a grid. The grid consists of `0`s and `1`s, where `0` represents water and `1` represents land. An island is defined as a maximal group of `1`s connected 4-directionally, meaning only up, down, left, and right connections are considered, not diagonally. The function signature is `def count_islands(grid: list) -> int:`. First, I should think about how to approach this problem. One common method to solve island problems is using Depth-First Search (DFS) or Breadth-First Search (BFS). Both can be used to traverse the grid and identify connected components. Let me consider using DFS for this implementation. The idea is to iterate through each cell in the grid. When I encounter a `1`, it means I've found the start of a new island. I'll need to mark this cell as visited to avoid counting it multiple times. Then, I'll perform a DFS from this cell, exploring all its 4-directionally connected `1`s and marking them as visited as well. I'll need to handle edge cases, such as an empty grid or a grid with no `1`s. Also, I should ensure that the function handles grids of varying sizes, including non-square grids. Another consideration is the time and space complexity. Since I'm visiting each cell once, the time complexity should be O(M*N), where M is the number of rows and N is the number of columns in the grid. The space complexity will also be O(M*N) in the worst case due to the recursion stack in DFS. I should also think about how to represent the grid. It's given as a list of lists, so I'll need to iterate through each sublist to access individual cells. Let me outline the steps: 1. Initialize a counter for islands. 2. Iterate through each cell in the grid. 3. When a `1` is found, increment the island counter and perform DFS to mark all connected `1`s as visited. 4. Return the total number of islands. Now, I'll start writing the code. I'll define a helper function `dfs` that takes the gr
Cut here for length. The model page has the full answer.
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 7/10.
Medgemma 27B IT pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
num_islands = 0
def dfs(row, col):
if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == 0:
return
grid[row][col] = 0 # Mark as visited (sink the island)
# Explore adjacent cells
dfs(row + 1, col)
dfs(row - 1, col)
dfs(row, col + 1)
dfs(row, col - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
num_islands += 1
dfs(i, j)
return num_islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
medgemma-4b-it pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
num_islands = 0
def dfs(row, col):
if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == 0:
return
grid[row][col] = 0 # Mark as visited
# Explore neighbors
dfs(row + 1, col)
dfs(row - 1, col)
dfs(row, col + 1)
dfs(row, col - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
num_islands += 1
dfs(i, j)
return num_islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Medgemma1.5 4B IT pass score 1.00
Answer (generation ok, stop)
```python
import collections
def count_islands(grid: list) -> int:
"""
Counts the number of islands in a grid of 0s (water) and 1s (land).
An island is a maximal group of 1s connected 4-directionally.
Args:
grid: A list of lists representing the grid.
Returns:
The number of islands.
"""
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
visited = set()
island_count = 0
def bfs(r, c):
"""Performs Breadth-First Search to find all connected land cells."""
if not (0 <= r < rows and 0 <= c < cols) or \
(r, c) in visited or \
grid[r][c] == 0:
return
q = collections.deque([(r, c)])
visited.add((r, c))
while q:
curr_r, curr_c = q.popleft()
# Explore neighbors
for dr, dc in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
nr, nc = curr_r + dr, curr_c + dc
if 0 <= nr < rows and 0 <= nc < cols and \
(nr, nc) not in visited and \
grid[nr][nc] == 1:
visited.add((nr, nc))
q.append((nr, nc))
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and (r, c) not in visited:
island_count += 1
bfs(r, c) # Explore the entire island
return island_count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 10/10.
Meta-Llama-3-8B-Instruct pass score 1.00
Answer (generation ok, stop)
```
def count_islands(grid: list) -> int:
if not grid:
return 0
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(grid, i, j)
count += 1
return count
def dfs(grid, i, j):
if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = '#'
dfs(grid, i-1, j)
dfs(grid, i+1, j)
dfs(grid, i, j-1)
dfs(grid, i, j+1)
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 7/10.
Ministral 3 14B Instruct (2512) pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
count += 1
_dfs(grid, i, j, rows, cols)
return count
def _dfs(grid: list, i: int, j: int, rows: int, cols: int) -> None:
if i < 0 or i >= rows or j < 0 or j >= cols or grid[i][j] != 1:
return
grid[i][j] = 0 # Mark as visited
_dfs(grid, i + 1, j, rows, cols)
_dfs(grid, i - 1, j, rows, cols)
_dfs(grid, i, j + 1, rows, cols)
_dfs(grid, i, j - 1, rows, cols)
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Ministral 3 3B Instruct 2512 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False for _ in range(cols)] for _ in range(rows)]
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
count += 1
stack = [(i, j)]
visited[i][j] = True
while stack:
x, y = stack.pop()
for dx, dy in directions:
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols:
if grid[nx][ny] == 1 and not visited[nx][ny]:
visited[nx][ny] = True
stack.append((nx, ny))
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 10/10.
Ministral 3 8B Instruct 2512 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False for _ in range(cols)] for _ in range(rows)]
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
count += 1
stack = [(i, j)]
visited[i][j] = True
while stack:
x, y = stack.pop()
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols:
if grid[nx][ny] == 1 and not visited[nx][ny]:
visited[nx][ny] = True
stack.append((nx, ny))
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 10/10.
Mistral 7B Instruct V0.3 pass score 1.00
Answer (generation ok, stop)
Here's the Python function for counting islands in a grid:
```python
def count_islands(grid):
visited = set()
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1 and (i, j) not in visited:
d4 = [(0, 1), (1, 0), (-1, 0), (0, -1)]
stack = [(i, j)]
visited.add((i, j))
while stack:
x, y = stack.pop()
for dx, dy in d4:
nx, ny = x + dx, y + dy
if 0 <= nx < len(grid) and 0 <= ny < len(grid[0]) and grid[nx][ny] == 1 and (nx, ny) not in visited:
stack.append((nx, ny))
visited.add((nx, ny))
count += 1
return count
```
This function uses depth-first search (DFS) to traverse the grid and count the number of islands. It initializes a visited set to keep track of the visited cells and a count variable to store the number of islands. The function iterates through the grid and checks each cell that has a value of 1 and hasn't been visited yet. If such a cell is found, it performs a DFS to explore the connected components of the island and increments the count.
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 7/10.
Mistral Nemo 12B Instruct 2407 pass score 1.00
Answer (generation ok, stop)
def count_islands(grid):
if not grid: return 0
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += 1
dfs(grid, i, j)
return count
def dfs(grid, i, j):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(grid, i-1, j)
dfs(grid, i+1, j)
dfs(grid, i, j-1)
dfs(grid, i, j+1)
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 7/10.
Mistral Small 22B Instruct 2409 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
count = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0:
return
grid[r][c] = 0
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
count += 1
dfs(r, c)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Mistral Small 24B Instruct 2501 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
def dfs(r, c):
grid[r][c] = 0
for dr, dc in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
nr, nc = r + dr, c + dc
if 0 <= nr < len(grid) and 0 <= nc < len(grid[0]) and grid[nr][nc] == 1:
dfs(nr, nc)
count = 0
for r in range(len(grid)):
for c in range(len(grid[0])):
if grid[r][c] == 1:
count += 1
dfs(r, c)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Mistral Small 3.2 24B Instruct (2506) pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows = len(grid)
cols = len(grid[0])
count = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] != 1:
return
grid[r][c] = -1 # Mark as visited
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
count += 1
dfs(r, c)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
This answer on the page of Mistral Small 3.2 24B Instruct (2506)
Mistral Small3.1 24B Instruct 2503 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or visited[r][c] or grid[r][c] == 0:
return
visited[r][c] = True
dfs(r - 1, c)
dfs(r + 1, c)
dfs(r, c - 1)
dfs(r, c + 1)
island_count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
island_count += 1
return island_count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
This answer on the page of Mistral Small3.1 24B Instruct 2503
Mistral-7B-Instruct-v0.2 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
if not grid: return 0
m, n = len(grid), len(grid[0])
visited = [[False]*n for _ in range(m)]
count = 0
for i in range(m):
for j in range(n):
if not visited[i][j] and grid[i][j]:
dfs(i, j, grid, visited)
count += 1
return count
def dfs(i, j, grid, visited):
if i<0 or i>=len(grid) or j<0 or j>=len(grid[0]) or visited[i][j] or not grid[i][j]: return
visited[i][j] = True
dfs(i+1, j, grid, visited)
dfs(i-1, j, grid, visited)
dfs(i, j+1, grid, visited)
dfs(i, j-1, grid, visited)
```
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 8/10.
Muse Glimmer 30B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
dirs = [(1,0), (-1,0), (0,1), (0,-1)]
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
count += 1
stack = [(i, j)]
visited[i][j] = True
while stack:
x, y = stack.pop()
for dx, dy in dirs:
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols:
if grid[nx][ny] == 1 and not visited[nx][ny]:
visited[nx][ny] = True
stack.append((nx, ny))
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Nanbeige_Nanbeige4.3-33B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
count = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0:
return
grid[r][c] = 0
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
count += 1
dfs(r, c)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Nemotron 3.5 Bolt E4B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
num_islands = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0:
return
# Mark as visited by changing '1' to '0'
grid[r][c] = 0
# Explore neighbors
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
num_islands += 1
# Start DFS to sink the entire island
dfs(r, c)
return num_islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Nemotron 3.5 Lightning 30B A3B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols:
return
if grid[r][c] == 0 or visited[r][c]:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Nemotron Cascade 2 30B A3B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
stack = [(r, c)]
while stack:
x, y = stack.pop()
if x < 0 or x >= rows or y < 0 or y >= cols:
continue
if visited[x][y] or grid[x][y] == 0:
continue
visited[x][y] = True
stack.extend([(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)])
islands = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
dfs(i, j)
islands += 1
return islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 10/10.
Nemotron Mini 4B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
count = 0
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 1:
count += 1
# DFS to find all connected 1s
def dfs(i, j):
grid[i][j] = 0
if i > 0 and grid[i-1][j] == 1:
dfs(i-1, j)
if i < len(grid)-1 and grid[i+1][j] == 1:
dfs(i+1, j)
if j > 0 and grid[i][j-1] == 1:
dfs(i, j-1)
if j < len(grid[i])-1 and grid[i][j+1] == 1:
dfs(i, j+1)
dfs(i, j)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Nemotron3 33B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
if (r < 0 or r >= rows or c < 0 or c >= cols or
visited[r][c] or grid[r][c] == 0):
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
NVIDIA Nemotron 3 Nano 30B-A3B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
if (
r < 0 or r >= rows or
c < 0 or c >= cols or
visited[r][c] or
grid[r][c] == 0
):
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
islands = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
islands += 1
dfs(i, j)
return islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Olmo 3.1 32B Instruct pass score 1.00
Answer (generation ok, stop)
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0:
return
grid[r][c] = 0
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
dfs(r, c)
count += 1
return count
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Olmo-3-7B-Instruct pass score 1.00
Answer (generation ok, stop)
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False]*cols for _ in range(rows)]
count = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or visited[r][c] or grid[r][c] != 1:
return
visited[r][c] = True
dfs(r-1,c)
dfs(r+1,c)
dfs(r,c-1)
dfs(r,c+1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
count += 1
dfs(i,j)
return count
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Olmo-3-7B-Think pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Opencoder 1.5B Instruct pass score 1.00
Answer (generation ok, stop)
Here is the code to solve this problem:
```python
from typing import List
def count_islands(grid: List[List[int]]) -> int:
if not grid:
return 0
def dfs(i: int, j: int) -> None:
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(i-1, j)
dfs(i+1, j)
dfs(i, j-1)
dfs(i, j+1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 7/10.
Openthinker 7B V2 pass score 1.00
Answer (generation ok, stop)
<think> Okay, I need to write a Python function called count_islands that takes a grid as input and returns the number of islands. The grid consists of 0s and 1s, where 1s are land and 0s are water. An island is a group of 1s connected 4-directionally, meaning up, down, left, right. Diagonals don't count. Hmm, so the problem is similar to the number of connected components in a graph, where each cell is a node and edges exist between adjacent cells (up, down, left, right) if they are both land. So the approach here is to traverse the grid and for each land cell that hasn't been visited yet, perform a search (like BFS or DFS) to mark all connected land cells as part of the same island. Each time we start a new search, that's a new island. Let me think about the steps: 1. Iterate through each cell in the grid. 2. When a cell with value 1 is found that hasn't been visited, increment the island count. 3. Then, perform a search (BFS or DFS) to mark all connected 1s as visited. But how to track visited cells? Since the grid is made of 0s and 1s, maybe we can modify the grid in place by changing visited 1s to 0s. That way, we don't need extra space for a visited matrix. That's efficient. So the plan is: - Initialize a count to 0. - Loop through each row and column in the grid. - For each cell (i,j): - If grid[i][j] is 1, increment count by 1. - Then, perform a BFS or DFS to mark all connected 1s as visited (by setting them to 0). Which traversal method is better here? Both are acceptable. Let's pick BFS for this example. Wait, but modifying the grid might not be allowed if the function is supposed to leave the grid as is. But the problem statement doesn't mention that. The examples show that the grid is passed, but in Python, lists are mutable. So modifying the grid in place is acceptable here, as it's allowed to change the input. But maybe the user expects the grid to remain unchanged. Wait, but in the examples, the function returns the count, and the grid
Cut here for length. The model page has the full answer.
Judge scores: anthropic/claude-haiku-5.5 4/10; openai/gpt-5.6-luna 8/10.
Ornith 9B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
count = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] != 1:
return
grid[r][c] = 0
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Ornith-1.5-9B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols:
return
if grid[r][c] == 0 or visited[r][c]:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Phi 2.7B Chat V2 pass score 1.00
Answer (generation ok, stop)
def count_islands(grid):
if not grid:
return 0
rows = len(grid)
cols = len(grid[0])
visited = [[False for _ in range(cols)] for _ in range(rows)]
def dfs(row, col):
if row < 0 or row >= rows or col < 0 or col >= cols or visited[row][col] or grid[row][col] == 0:
return
visited[row][col] = True
dfs(row - 1, col)
dfs(row + 1, col)
dfs(row, col - 1)
dfs(row, col + 1)
count = 0
for row in range(rows):
for col in range(cols):
if not visited[row][col] and grid[row][col] == 1:
dfs(row, col)
count += 1
return count
# Example usage:
grid = [[1,1,0],[0,1,0],[0,0,1]]
print(count_islands(grid)) # Output: 2
grid = [[0,0],[0,0]]
print(count_islands(grid)) # Output: 0
# Explanation:
# The function count_islands takes a grid as input and returns the number of islands in the grid.
# It uses a depth-first search (DFS) algorithm to traverse the grid and mark visited cells.
# The DFS function takes the row and column indices of a cell as input and recursively visits all connected land cells.
# The count variable keeps track of the number of islands found.
# The function returns the count of islands in the grid.
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 7/10.
Phi-3-mini-4k-instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
if not grid or not grid[0]:
return 0
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += employee_count_islands(grid, i, j)
return count
def employee_count_islands(grid, i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return 0
grid[i][j] = 0
employee_count_islands(grid, i + 1, j)
employee_count_islands(grid, i - 1, j)
employee_count_islands(grid, i, j + 1)
employee_count_islands(grid, i, j - 1)
return 1
```
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 8/10.
Phi-3.5-mini-instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
if not grid or not grid[0]:
return 0
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i+1, j)
dfs(i-1, j)
dfs(i, j+1)
dfs(i, j-1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
phi-4 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(x, y):
if x < 0 or x >= len(grid) or y < 0 or y >= len(grid[0]) or grid[x][y] == 0:
return
grid[x][y] = 0
dfs(x + 1, y)
dfs(x - 1, y)
dfs(x, y + 1)
dfs(x, y - 1)
if not grid:
return 0
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Phi-4-mini-instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(r, c):
if r < 0 or c < 0 or r >= len(grid) or c >= len(grid[r]) or grid[r][c] == 0:
return
grid[r][c] = 0
dfs(r-1, c)
dfs(r+1, c)
dfs(r, c-1)
dfs(r, c+1)
count = 0
for r in range(len(grid)):
for c in range(len(grid[r])):
if grid[r][c] == 1:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Phi3 14B Medium 128K Instruct pass score 1.00
Answer (generation ok, stop)
def count_islands(grid):
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
for x, y in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]:
dfs(x, y)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Phi3 3.8B Mini 128K Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i+1, j)
dfs(i-1, j)
dfs(i, j+1)
dfs(i, j-1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Qwen2 1.5B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(x, y):
if 0 <= x < len(grid) and 0 <= y < len(grid[0]) and grid[x][y] == 1:
grid[x][y] = 0
dfs(x - 1, y)
dfs(x + 1, y)
dfs(x, y - 1)
dfs(x, y + 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Qwen2 Math 7B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(x, y):
if 0 <= x < len(grid) and 0 <= y < len(grid[0]) and grid[x][y] == 1:
grid[x][y] = 0
dfs(x+1, y)
dfs(x-1, y)
dfs(x, y+1)
dfs(x, y-1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Qwen2-VL-7B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid):
def dfs(r, c):
if r < 0 or c < 0 or r >= len(grid) or c >= len(grid[0]) or grid[r][c] == 0:
return
grid[r][c] = 0
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
count = 0
for r in range(len(grid)):
for c in range(len(grid[0])):
if grid[r][c] == 1:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Qwen2.5 0.5B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
"""
Counts the number of islands in a grid of 0s and 1s.
Args:
grid: A 2D list representing the grid of 0s and 1s.
Returns:
An integer representing the number of islands.
"""
def dfs(x, y):
if x < 0 or x >= len(grid) or y < 0 or y >= len(grid[0]) or grid[x][y] == 0:
return
grid[x][y] = 0 # Mark the cell as visited
dfs(x + 1, y) # Explore the right neighbor
dfs(x - 1, y) # Explore the left neighbor
dfs(x, y + 1) # Explore the down neighbor
dfs(x, y - 1) # Explore the up neighbor
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += 1
dfs(i, j) # Start DFS from each island cell
return count
# Example usage
grid = [[1,1,0,0,0],[0,1,1,0,0],[0,0,0,1,0]]
print(count_islands(grid)) # Output: 2
```
This solution defines a function `count_islands` that takes a 2D list `grid` as input and returns the number of islands in the grid. It uses a depth-first search (DFS) approach to explore each island and mark visited cells. The `dfs` function is called for each cell in the grid, and the total count of islands is returned.
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 7/10.
Qwen2.5 1.5B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] == 1:
grid[i][j] = 0 # Mark as visited
dfs(i + 1, j) # Down
dfs(i - 1, j) # Up
dfs(i, j + 1) # Right
dfs(i, j - 1) # Left
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Qwen2.5 Coder 0.5B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(x, y):
if x < 0 or x >= len(grid) or y < 0 or y >= len(grid[0]) or grid[x][y] == 0:
return
grid[x][y] = 0
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
dfs(x + dx, y + dy)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Qwen2.5 Coder 1.5B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0 # Mark the cell as visited
dfs(i + 1, j) # Explore down
dfs(i - 1, j) # Explore up
dfs(i, j + 1) # Explore right
dfs(i, j - 1) # Explore left
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Qwen2.5-14B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
island_count = 0
def dfs(r, c):
if (
r < 0
or r >= rows
or c < 0
or c >= cols
or grid[r][c] == 0
or visited[r][c]
):
return
visited[r][c] = True
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
for dr, dc in directions:
dfs(r + dr, c + dc)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
island_count += 1
return island_count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 10/10.
Qwen2.5-32B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i+1, j)
dfs(i-1, j)
dfs(i, j+1)
dfs(i, j-1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 7/10.
Qwen2.5-3B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = set()
def dfs(r, c):
if (
r not in range(rows) or
c not in range(cols) or
(r, c) in visited or
grid[r][c] == 0
):
return
visited.add((r, c))
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
for dr, dc in directions:
dfs(r + dr, c + dc)
island_count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and (r, c) not in visited:
dfs(r, c)
island_count += 1
return island_count
```
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 9/10.
Qwen2.5-7B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = set()
def dfs(r, c):
if (r, c) in visited or r < 0 or c < 0 or r == rows or c == cols or grid[r][c] == 0:
return
visited.add((r, c))
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
for dr, dc in directions:
dfs(r + dr, c + dc)
island_count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and (r, c) not in visited:
dfs(r, c)
island_count += 1
return island_count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Qwen2.5-Coder-14B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Qwen2.5-Coder-32B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Qwen2.5-Coder-3B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0 # Mark the cell as visited
dfs(i + 1, j) # Explore down
dfs(i - 1, j) # Explore up
dfs(i, j + 1) # Explore right
dfs(i, j - 1) # Explore left
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 8/10.
Qwen2.5-Coder-7B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i-1, j)
dfs(i+1, j)
dfs(i, j-1)
dfs(i, j+1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Qwen2.5-VL-32B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
count = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0 or visited[r][c]:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Qwen2.5-VL-3B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(r, c):
if r < 0 or c < 0 or r >= len(grid) or c >= len(grid[0]) or grid[r][c] == 0:
return
grid[r][c] = 0
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
count = 0
for r in range(len(grid)):
for c in range(len(grid[0])):
if grid[r][c] == 1:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Qwen2.5-VL-7B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = -1 # Mark as visited
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 8/10.
Qwen3 VL 2B Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
islands = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
islands += 1
# Do DFS to mark all connected 1s as visited
stack = [(r, c)]
visited[r][c] = True
while stack:
row, col = stack.pop()
# Check all 4 directions
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
for dr, dc in directions:
new_row, new_col = row + dr, col + dc
if (0 <= new_row < rows and 0 <= new_col < cols and
not visited[new_row][new_col] and grid[new_row][new_col] == 1):
visited[new_row][new_col] = True
stack.append((new_row, new_col))
return islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 10/10.
Qwen3-14B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False for _ in range(cols)] for _ in range(rows)]
count = 0
def dfs(r, c):
if r < 0 or c < 0 or r >= rows or c >= cols or grid[r][c] == 0 or visited[r][c]:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Qwen3-30B-A3B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False for _ in range(cols)] for _ in range(rows)]
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0 or visited[r][c]:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Qwen3-30B-A3B-Instruct-2507 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
islands = 0
def dfs(r, c):
if (r < 0 or r >= rows or c < 0 or c >= cols or
visited[r][c] or grid[r][c] == 0):
return
visited[r][c] = True
# Explore 4-directionally
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
dfs(i, j)
islands += 1
return islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Qwen3-32B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False for _ in range(cols)] for _ in range(rows)]
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0 or visited[r][c]:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Qwen3-4B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False for _ in range(cols)] for _ in range(rows)]
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
# Perform BFS to mark all connected 1s as visited
from collections import deque
q = deque()
q.append((i, j))
visited[i][j] = True
while q:
x, y = q.popleft()
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols and not visited[nx][ny] and grid[nx][ny] == 1:
visited[nx][ny] = True
q.append((nx, ny))
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Qwen3-4B-Instruct-2507 pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
visited = [[False] * cols for _ in range(rows)]
count = 0
def dfs(r, c):
if (r < 0 or r >= rows or c < 0 or c >= cols or
visited[r][c] or grid[r][c] == 0):
return
visited[r][c] = True
# Explore all 4 directions
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Qwen3-8B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
count = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0:
return
grid[r][c] = 0
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
count += 1
dfs(r, c)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
Qwen3-Coder-30B-A3B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
islands = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0 or visited[r][c]:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
dfs(i, j)
islands += 1
return islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 10/10.
Qwen3-Coder-Next pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols:
return
if grid[r][c] == 0 or visited[r][c]:
return
visited[r][c] = True
dfs(r+1, c)
dfs(r-1, c)
dfs(r, c+1)
dfs(r, c-1)
count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 10/10.
Qwen3-VL-30B-A3B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
count = 0
def dfs(r, c):
if (r < 0 or r >= rows or c < 0 or c >= cols or
grid[r][c] == 0 or visited[r][c]):
return
visited[r][c] = True
# Explore all 4 directions
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Qwen3-VL-32B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
count = 0
def dfs(r, c):
if (r < 0 or r >= rows or c < 0 or c >= cols or
visited[r][c] or grid[r][c] == 0):
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Qwen3-VL-4B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
count = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0 or visited[r][c]:
return
visited[r][c] = True
# Check 4 directions: up, down, left, right
for dr, dc in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
dfs(r + dr, c + dc)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
count += 1
dfs(i, j)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Qwen3-VL-8B-Instruct pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = set()
islands = 0
def dfs(r, c):
if (r < 0 or r >= rows or c < 0 or c >= cols or
grid[r][c] == 0 or (r, c) in visited):
return
visited.add((r, c))
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and (r, c) not in visited:
dfs(r, c)
islands += 1
return islands
```
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 9/10.
Qwen3.5 2B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
visited = [[False for _ in range(cols)] for _ in range(rows)]
islands = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or visited[r][c] or grid[r][c] == 0:
return
visited[r][c] = True
for dr, dc in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
dfs(r + dr, c + dc)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
islands += 1
dfs(r, c)
return islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Qwen3.5-27B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols:
return
if grid[r][c] == 0 or visited[r][c]:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Qwen3.5-35B-A3B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols:
return
if visited[r][c] or grid[r][c] == '0' or grid[r][c] == 0:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
count += 1
dfs(r, c)
return count
```
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 8/10.
Qwen3.5-4B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
visited = [[False for _ in range(cols)] for _ in range(rows)]
count = 0
def dfs(r, c):
stack = [(r, c)]
while stack:
curr_r, curr_c = stack.pop()
if curr_r < 0 or curr_r >= rows or curr_c < 0 or curr_c >= cols:
continue
if grid[curr_r][curr_c] == 0 or visited[curr_r][curr_c]:
continue
visited[curr_r][curr_c] = True
stack.append((curr_r + 1, curr_c))
stack.append((curr_r - 1, curr_c))
stack.append((curr_r, curr_c + 1))
stack.append((curr_r, curr_c - 1))
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 10/10; openai/gpt-5.6-luna 10/10.
Qwen3.5-9B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
island_count = 0
def dfs(r, c):
stack = [(r, c)]
visited[r][c] = True
while stack:
curr_r, curr_c = stack.pop()
for dr, dc in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
nr, nc = curr_r + dr, curr_c + dc
if 0 <= nr < rows and 0 <= nc < cols:
if not visited[nr][nc] and grid[nr][nc] == 1:
visited[nr][nc] = True
stack.append((nr, nc))
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
island_count += 1
dfs(r, c)
return island_count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 10/10.
Qwen3.6-27B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols:
return
if grid[r][c] == 0 or visited[r][c]:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 8/10; openai/gpt-5.6-luna 9/10.
Qwen3.6-35B-A3B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
stack = [(r, c)]
while stack:
curr_r, curr_c = stack.pop()
if 0 <= curr_r < rows and 0 <= curr_c < cols and not visited[curr_r][curr_c] and grid[curr_r][curr_c] == 1:
visited[curr_r][curr_c] = True
stack.extend([
(curr_r - 1, curr_c),
(curr_r + 1, curr_c),
(curr_r, curr_c - 1),
(curr_r, curr_c + 1)
])
count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 10/10.
Qwen3.8-27B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols:
return
if visited[r][c] or grid[r][c] == 0:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
count += 1
dfs(i, j)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
QwQ 32B pass score 1.00
Answer (generation ok, stop)
```python
from collections import deque
def count_islands(grid: list) -> int:
if not grid:
return 0
rows = len(grid)
cols = len(grid[0])
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
count += 1
queue = deque()
queue.append((i, j))
grid[i][j] = 0
while queue:
x, y = queue.popleft()
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
nx = x + dx
ny = y + dy
if 0 <= nx < rows and 0 <= ny < cols and grid[nx][ny] == 1:
grid[nx][ny] = 0
queue.append((nx, ny))
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Sage-A1.5-Q4_K_M pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
count = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols:
return
if visited[r][c] or grid[r][c] == 0:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
count += 1
dfs(r, c)
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 8/10.
Smallthinker 3B Preview pass score 1.00
Answer (generation ok, stop)
Alright, I need to write a function that counts the number of islands in a grid. The grid is made up of 0s and 1s, where 0s are water and 1s are land. An island is a group of connected 1s (up, down, left, right) that are not connected to any other 1s. The function should return the number of such islands. First, I need to understand what constitutes an island. An island is a connected component of 1s, and it's maximal, meaning it can't be broken down into smaller groups without separating some 1s. So, I need to traverse the grid and identify these connected components. I think the best way to approach this is using a depth-first search (DFS) or breadth-first search (BFS). Both can be used to explore each island and mark the cells as visited once they're part of an island. Let me consider using DFS because it's simpler to implement recursively. I'll need to iterate through each cell in the grid. When I find a cell with a 1, I'll start a DFS from that cell to explore the entire island, marking all connected 1s as visited. I should also keep a visited matrix to avoid recounting cells that are already part of an island. But since the grid is given as a list of lists, I need to make sure that I'm not modifying the original grid, as that could affect the function's behavior. Wait, actually, the problem doesn't specify whether the grid can be modified or not. To be safe, I should avoid modifying the grid. So, I'll create a separate visited matrix to keep track of visited cells. But creating a separate visited matrix would require additional space, which might not be efficient. Is there a way to mark cells as visited without using extra space? One way is to modify the grid itself, but as I thought earlier, it's better to avoid modifying the input grid to keep the function pure and avoid side effects. Alternatively, I can use a set to keep track of visited cells, but that would require additional space proportional to the number of cells. Maybe I can use a tuple to
Cut here for length. The model page has the full answer.
Judge scores: anthropic/claude-haiku-5.5 2/10; openai/gpt-5.6-luna 7/10.
SmolLM3-3B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
count = 0
rows, cols = len(grid), len(grid[0])
visited = [[False for _ in range(cols)] for _ in range(rows)]
def dfs(i, j):
if i < 0 or j < 0 or i >= rows or j >= cols or grid[i][j] == 0 or visited[i][j]:
return
visited[i][j] = True
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Spark-X2.6-4B pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
visited = [[False] * cols for _ in range(rows)]
count = 0
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols:
return
if visited[r][c] or grid[r][c] == 0:
return
visited[r][c] = True
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
Translategemma 12B IT pass score 1.00
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
num_islands = 0
def dfs(row, col):
if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == 0:
return
grid[row][col] = 0 # Mark as visited
# Explore adjacent cells
dfs(row + 1, col)
dfs(row - 1, col)
dfs(row, col + 1)
dfs(row, col - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
num_islands += 1
dfs(i, j)
return num_islands
```
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 9/10.
YI 34B Chat V1.5 pass score 1.00
Answer (generation ok, stop)
def count_islands(grid):
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = -1
dfs(i - 1, j)
dfs(i + 1, j)
dfs(i, j - 1)
dfs(i, j + 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
YI 9B Chat V1.5 pass score 1.00
Answer (generation ok, stop)
def count_islands(grid: list) -> int:
def dfs(i: int, j: int):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(i - 1, j)
dfs(i + 1, j)
dfs(i, j - 1)
dfs(i, j + 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
Judge scores: anthropic/claude-haiku-5.5 9/10; openai/gpt-5.6-luna 7/10.
YI Coder 1.5B Chat pass score 1.00
Answer (generation ok, stop)
Here is a Python solution using depth-first search (DFS).
```python
def count_islands(grid):
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(i-1, j)
dfs(i+1, j)
dfs(i, j-1)
dfs(i, j+1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
This function works by iterating over each cell in the grid. If a cell contains a `1`, it calls a DFS to mark all connected `1`s as `0`s. This is done by recursively calling the DFS on the neighboring cells (up/down/left/right).
The DFS is stopped when it encounters a cell that is not a `1` or when it has visited all neighboring cells.
After visiting all cells, the function returns the number of islands found.
The time complexity of this function is O(m*n), where m is the number of rows and n is the number of columns in the grid. This is because in the worst case, the function visits every cell in the grid once.
The space complexity is O(m*n), where m is the number of rows and n is the number of columns in the grid. This is because in the worst case, the function uses a recursive call stack to store the cells that need to be visited.
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 7/10.
YI Coder 9B Chat pass score 1.00
Answer (generation ok, stop)
def count_islands(grid):
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(i - 1, j)
dfs(i + 1, j)
dfs(i, j - 1)
dfs(i, j + 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
# Test cases
print(count_islands([[1,1,0],[0,1,0],[0,0,1]])) # -> 2
print(count_islands([[0,0],[0,0]])) # -> 0
print(count_islands([[1,0,1],[0,1,0],[1,0,1]])) # -> 5
print(count_islands([[1,1,1],[1,0,1],[1,1,1]])) # -> 1
print(count_islands([[0,0,0],[0,1,0],[0,0,0]])) # -> 1
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 7/10.
Apertus-8B-Instruct-2509 88% score 0.88
Answer (generation ok, stop)
def count_islands(grid: list) -> int:
islands = 0
rows, cols = len(grid), len(grid[0])
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
queue = [(r, c)]
while queue:
x, y = queue.pop(0)
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols and grid[nx][ny] == 1:
grid[nx][ny] = 0
queue.append((nx, ny))
islands += 1
return islands
Judge scores: anthropic/claude-haiku-5.5 6/10; openai/gpt-5.6-luna 6/10.
Codegemma 7B Instruct V1.1 88% score 0.88
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
rows, cols = len(grid), len(grid[0])
count = 0
def dfs(i, j):
if i < 0 or i >= rows or j < 0 or j >= cols or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
```
Judge scores: anthropic/claude-haiku-5.5 4/10; openai/gpt-5.6-luna 6/10.
Tulu3 8B 88% score 0.88
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(r, c):
grid[r][c] = 0
for dr, dc in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
nr, nc = r + dr, c + dc
if 0 <= nr < R and 0 <= nc < C and grid[nr][nc] == 1:
dfs(nr, nc)
R, C = len(grid), len(grid[0])
count = 0
for r in range(R):
for c in range(C):
if grid[r][c] == 1:
dfs(r, c)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 7/10; openai/gpt-5.6-luna 6/10.
Mathstral 7B V0.1 75% score 0.75
Answer (generation ok, stop)
1. First, we need to define the function `count_islands` that takes a grid as input.
2. Inside the function, we initialize a variable `count` to 0, which will keep track of the number of islands.
3. We then iterate over each row in the grid using a nested loop.
4. For each cell in the grid, we check if it is a land cell (i.e., its value is 1). If it is, we increment the `count` variable.
5. We then recursively call the `count_islands` function on the neighboring cells (up, down, left, and right) to count the number of connected land cells.
6. Finally, we return the `count` variable, which represents the total number of islands in the grid.
Here is the Python code for the `count_islands` function:
```python
def count_islands(grid):
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += 1
grid[i][j] = 0
if i > 0:
grid[i-1][j] = 0
if i < len(grid)-1:
grid[i+1][j] = 0
if j > 0:
grid[i][j-1] = 0
if j < len(grid[0])-1:
grid[i][j+1] = 0
return count
```
You can test the function with the provided examples:
```python
print(count_islands([[1,1,0],[0,1,0],[0,0,1]])) # Output: 2
print(count_islands([[0,0],[0,0]])) # Output: 0
```
Judge scores: anthropic/claude-haiku-5.5 3/10; openai/gpt-5.6-luna 5/10.
Gemma 2B Instruct V1.1 38% score 0.38
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
connected = 0
for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
if 0 <= i + dx < len(grid) and 0 <= j + dy < len(grid[0]) and grid[i + dx][j + dy] == 1:
connected += 1
count += connected
return count
```
Judge scores: anthropic/claude-haiku-5.5 2/10; openai/gpt-5.6-luna 4/10.
Granite Code 3B Instruct 38% score 0.38
Answer (generation ok, stop)
Here is a Python function that implements this approach:
```python
def count_islands(grid):
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += 1
grid[i][j] = 0
dfs(grid, i, j)
return count
def dfs(grid, i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(grid, i + 1, j)
dfs(grid, i - 1, j)
dfs(grid, i, j + 1)
dfs(grid, i, j - 1)
```
The function `count_islands` takes a grid as input and returns the number of islands. It initializes a counter variable `count` to keep track of the number of islands. It then iterates over each cell in the grid and checks if it is a land cell (value `1`). If it is, it increments the counter and marks the cell as visited using the `dfs` function. The `dfs` function recursively explores the neighboring cells (up, down, left, and right) of the current cell. If a neighbor is out of bounds or has already been visited, it skips it. Otherwise, it marks the neighbor as visited and recursively calls itself on the neighbor. This process continues until all cells have been visited.
Judge scores: anthropic/claude-haiku-5.5 3/10; openai/gpt-5.6-luna 4/10.
Qwen 0.5B Chat V1.5 38% score 0.38
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
count = 0
for row in grid:
for col in row:
if col == 1:
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 3/10; openai/gpt-5.6-luna 3/10.
Qwen 1.8B Chat V1.5 38% score 0.38
Answer (generation truncated, length)
```python
def count_islands(grid: list) -> int:
count = 0
for row in grid:
for cell in row:
if cell == 1:
count += 1
return count
```
Explanation:
- The function `count_islands` takes a 2D list `grid` as input.
- The variable `count` is initialized to 0.
- The outer loop `for row in grid:` iterates over each row in the input grid `grid`.
- The inner loop `for cell in row:` iterates over each cell in the current row of `grid`.
- Inside the inner loop, the condition `cell == 1` is checked.
- If the condition is true, it means that the current cell is equal to 1.
- In this case, the inner loop moves to the next cell in the current row.
- After the inner loop finishes, the condition `cell == 1` is no longer true.
- Inside the outer loop, the condition `row == 0` is checked.
- If the condition is true, it means that the current row is equal to 0.
- In this case, the outer loop moves to the next row in the input grid.
- After the outer loop finishes, the condition `row == 0` is no longer true.
- Inside the inner loop, the condition `cell == 0` is checked.
- If the condition is true, it means that the current cell is equal to 0.
- In this case, the inner loop moves to the next cell in the current row.
- After the inner loop finishes, the condition `cell == 0` is no longer true.
- Inside the outer loop, the condition `row == 1` is checked.
- If the condition is true, it means that the current row is equal to 1.
- In this case, the outer loop moves to the next row in the input grid.
- After the outer loop finishes, the condition `row == 1` is no longer true.
- Inside the inner loop, the condition `cell == 1` is checked.
- If the condition is true, it means that the current cell is equal to 1.
- In this case, the inner loop moves to the next cell in the current row.
- After the inner loop finishes, the condition `cell == 1` is no longer true.
- Inside the outer loop, the condition `row == 2` is checked.
- If the condition is tr
Cut here for length. The model page has the full answer.
Judge scores: anthropic/claude-haiku-5.5 3/10; openai/gpt-5.6-luna 4/10.
Qwen3 1.7B 38% score 0.38
Answer (generation ok, stop)
def count_islands(grid: list) -> int:
if not grid:
return 0
rows = len(grid)
cols = len(grid[0])
islands = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
islands += 1
mark_visited(grid, i, j)
return islands
def mark_visited(grid, i, j):
grid[i][j] = 0 # Mark as visited by setting to 0
Judge scores: anthropic/claude-haiku-5.5 3/10; openai/gpt-5.6-luna 4/10.
Smollm2 1.7B Instruct 38% score 0.38
Answer (generation ok, stop)
```python
def count_islands(grid):
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += dfs(grid, i, j)
return count
def dfs(grid, i, j):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] == 0:
return 0
grid[i][j] = 0
return 1 + dfs(grid, i-1, j) + dfs(grid, i+1, j) + dfs(grid, i, j-1) + dfs(grid, i, j+1)
```
Judge scores: anthropic/claude-haiku-5.5 4/10; openai/gpt-5.6-luna 4/10.
Stablelm Zephyr 3B 38% score 0.38
Answer (generation ok, stop)
Here is the Python code for the function `count_islands(grid)`:
```python
def count_islands(grid):
count = 0
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 1:
island = []
k = (i, j)
while k != (i, j) or grid[k[0]][k[1]] == 0:
k = (k[0] + 1, k[1] + 1)
island.append(k)
island.extend([(x, y) for x, y in [(k[0] - 1, k[1] + 2), (k[0] + 1, k[1] + 2), (k[0], k[1] - 1), (k[0], k[1] + 1)]])
count += 1
return count
```
This function iterates over the grid and checks if a cell contains land (1). If it does, it creates a list of coordinates that form the boundary of the island. It then extends this list with the coordinates of neighboring cells that are also part of the island. The function keeps track of the number of islands it has found and returns this count.
Judge scores: anthropic/claude-haiku-5.5 2/10; openai/gpt-5.6-luna 4/10.
Stablelm2 12B Chat 38% score 0.38
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(grid, i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return 0
grid[i][j] = 0 # Mark as visited
return 1 + dfs(grid, i - 1, j) + dfs(grid, i + 1, j) + dfs(grid, i, j - 1) + dfs(grid, i, j + 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += dfs(grid, i, j)
return count
```
This function uses depth-first search (DFS) to traverse and count islands in the grid. It iterates through each cell in the grid and if a cell contains `1`, it starts a DFS to explore all connected cells. The DFS function `dfs` checks if a cell is within the grid boundaries, is land (`1`), and hasn't been visited before. If all conditions are met, it marks the cell as visited and recursively explores its neighboring cells. The count of islands is incremented for each island found.
Judge scores: anthropic/claude-haiku-5.5 3/10; openai/gpt-5.6-luna 4/10.
Cogito 3B V1 Preview Llama 25% score 0.25
Answer (generation ok, stop)
def count_islands(grid: list) -> int:
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
def dfs(i, j):
if i < 0 or i >= rows or j < 0 or j >= cols or grid[i][j] == 0:
return
grid[i][j] = 0 # mark as visited
for di, dj in [(1,0), (-1,0), (0,1), (0,-1)]:
dfs(i+di, j+dj)
islands = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not any(grid[k][l] == 1 for k,l in [(i-1,j), (i+1,j), (i,j-1), (i,j+1)]):
dfs(i, j)
islands += 1
return islands
Judge scores: anthropic/claude-haiku-5.5 2/10; openai/gpt-5.6-luna 4/10.
Granite3 Moe 1B Instruct 25% score 0.25
Answer (generation ok, stop)
```python
def count_islands(grid):
def count_neighbors(i, j):
return 0 if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1 else 0
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += count_neighbors(i, j)
count_island()
return count
def count_island():
global count
count += 1
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1 and i != 0 and j != 0 and (i - 1, j) != (0, 0) and (i - 1, j) != (len(grid[0]) - 1, j) and (i, j - 1) != (0, 0) and (i, j - 1) != (len(grid) - 1, j):
count_neighbors(i, j)
```
Here's the function with the provided signature and examples:
```python
def count_islands(grid):
def count_neighbors(i, j):
return 0 if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1 else 0
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += count_neighbors(i, j)
count_island()
return count
```
And here's the `count_island` function to keep track of the number of islands:
```python
def count_island():
global count
count += 1
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1 and i != 0 and j != 0 and (i - 1, j) != (0, 0) and (i - 1, j) != (len(grid[0]) - 1, j) and (i, j - 1) != (0, 0) and (i, j - 1) != (len(grid) - 1, j):
count_neighbors(i, j)
```
You can call the `count_islands` function with a 2D list representing the grid to get the number of islands. For example:
```python
grid = [
[1, 1, 0],
[0, 1, 0],
[0, 0, 1]
]
print(count_islands(grid)) # Output: 2
```
Judge scores: anthropic/claude-haiku-5.5 2/10; openai/gpt-5.6-luna 3/10.
Lfm3 VL 2B 25% score 0.25
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
rows = len(grid)
cols = len(grid[0])
def dfs(x, y):
# Check boundaries and if the cell is land
if x < 0 or y < 0 or x >= rows or y >= cols or grid[x][y] != '1':
return
# Mark the current cell as visited by setting it to '0'
grid[x][y] = '0'
# Recursively visit all four directions
dfs(x-1, y)
dfs(x+1, y)
dfs(x, y-1)
dfs(x, y+1)
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == '1':
count += 1
dfs(i, j)
return count
```
Judge scores: anthropic/claude-haiku-5.5 2/10; openai/gpt-5.6-luna 4/10.
Llama-2-7b-chat-hf 25% score 0.25
Answer (generation ok, stop)
```
def count_islands(grid):
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += 1
if grid[i-1][j] == 1 or grid[i+1][j] == 1 or grid[j-1][i] == 1 or grid[j+1][i] == 1:
grid[i][j] = 0
return count
```
Explanation:
The function iterates through the grid, checking each cell if it is 1. If it is, it increments the count of islands. Then, it checks the four adjacent cells (up, down, left, right) and sets them to 0 if they are also part of an island. This ensures that the function only counts islands that are fully connected. Finally, it returns the count of islands.
Judge scores: anthropic/claude-haiku-5.5 2/10; openai/gpt-5.6-luna 3/10.
Mixtral 8x7B Instruct V0.1 25% score 0.25
Answer (generation ok, stop)
def count_islands(grid):
def dfs(r, c):
if (r < 0 or r >= len(grid) or c < 0 or c >= len(grid[0]) or
grid[r][c] != '1'):
return
grid[r][c] = '#'
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
count = 0
for r in range(len(grid)):
for c in range(len(grid[0])):
if grid[r][c] == '1':
dfs(r, c)
count += 1
return count
Judge scores: anthropic/claude-haiku-5.5 2/10; openai/gpt-5.6-luna 4/10.
Olmo2 13B 1124 Instruct 25% score 0.25
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def is_valid(x, y):
return 0 <= x < len(grid) and 0 <= y < len(grid[0])
def dfs(x, y):
if x < 0 or y < 0 or x >= len(grid) or y >= len(grid[0]) or grid[x][y] != 1:
return 0
count = 1
for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
nx, ny = x + dx, y + dy
if is_valid(nx, ny) and grid[nx][ny] == 1:
count += dfs(nx, ny)
return count
total = 0
for row in grid:
for cell in row:
if cell == 1:
total += dfs(row, col)
return total
```
This function uses depth-first search (DFS) to traverse the grid and count the islands. It checks each cell and explores all its valid neighbors (up, down, left, right) if it's part of an island (represented by `1`). The `is_valid` function ensures that the coordinates are within the grid boundaries. The `dfs` function performs the depth-first search, incrementing the count for each island found. The `count_islands` function initializes a `total` count and iterates over the grid, calling `dfs` for each `1` it encounters.
Judge scores: anthropic/claude-haiku-5.5 2/10; openai/gpt-5.6-luna 3/10.
Qwen2 Math 1.5B Instruct 25% score 0.25
Answer (generation ok, stop)
To count the number of islands in a grid, we can use a depth-first search (DFS) algorithm. Here's a step-by-step explanation of how to implement this:
1. **Define the Function**: We'll define a function `count_islands` that takes a grid (a list of lists of integers) as input and returns the number of islands.
2. **Check for Island**: We'll iterate over each cell in the grid. If we find a cell with a value of 1 (land), we'll start a DFS from that cell to count all the connected 1s that form an island.
3. **Perform DFS**: During the DFS, we'll mark each visited cell as visited (we can do this by changing its value to 0). We'll recursively visit all its neighbors (up, down, left, right) that are also 1s.
4. **Count the Island**: Each time we start a new DFS from an unvisited 1, we'll count it as one island.
Here's the Python code that implements this:
```python
def count_islands(grid):
def dfs(i, j):
# Mark the current cell as visited (0)
grid[i][j] = 0
# Recursively call dfs for all its neighbors that are 1s
dfs(i-1, j) if 0 <= i-1 < len(grid) and grid[i-1][j] == 1 else ()
dfs(i+1, j) if 0 <= i+1 < len(grid) and grid[i+1][j] == 1 else ()
dfs(i, j-1) if 0 <= j-1 < len(grid[0]) and grid[i][j-1] == 1 else ()
dfs(i, j+1) if 0 <= j+1 < len(grid[0]) and grid[i][j+1] == 1 else ()
count = 0
for row in grid:
for col in row:
if col == 1:
dfs(col, row)
count += 1
return count
```
To use this function, you can call it like this:
```python
grid = [[1,1,0],[0,1,0],[0,0,1]]
print(count_islands(grid)) # Output: 2
```
This function will correctly count the number of islands in the given grid. The time complexity of this solution is O(m*n), where m is the number of rows and n is the number of columns in the grid, because we visit each cell exactly once. The space complexity is O(m*n) for the recursion stack.
Judge scores: anthropic/claude-haiku-5.5 2/10; openai/gpt-5.6-luna 3/10.
Sailor2 1B Chat 25% score 0.25
Answer (generation ok, stop)
**Function Definition:**
```python
def count_islands(grid: list) -> int:
"""
Counts the number of islands in a grid of water (`0`s) and land (`1`s).
An island is a maximal group of `1`s connected in 4-directional (up/down/left/right) only.
Parameters:
grid (list): A 2D list of integers representing the grid.
Returns:
int: The number of islands.
"""
# Initialize the count of islands to 0
island_count = 0
# Iterate through each row and column
for row in grid:
for col in range(len(row)):
# Check if the current row and column are islands
if row == 0 and col == 0:
island_count += 1
elif row == 1 and col == 0:
island_count += 1
elif row == 0 and col == 1:
island_count += 1
elif row == 1 and col == 1:
island_count += 1
return island_count
```
**Explanation:**
1. **Initialization**: We initialize `island_count` to 0 to keep track of the number of islands.
2. **Iterate Through Each Element**: For each row and column, we check if the current row and column are islands using the `==` operator.
3. **Count Islands**: If a row and column are islands, increment the `island_count`.
4. **Return the Result**: After processing all rows and columns, return the `island_count`.
This function efficiently counts the number of islands in a grid, ensuring that each island is counted correctly.
Judge scores: anthropic/claude-haiku-5.5 1/10; openai/gpt-5.6-luna 3/10.
Smollm2 135M Instruct 25% score 0.25
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
"""
Counts the number of islands in a grid.
Args:
grid (list): A list of `0`s and `1`s.
Returns:
int: The number of islands.
"""
count = 0
for row in grid:
for col in row:
if row == 1 and col == 1:
count += 1
return count
```
This function iterates through the grid, checking each row and column for each island. It increments a counter for each island, incrementing the count for each island. The function returns the final count of islands.
Example usage:
```python
print(count_islands([[1,1,0],[0,1,0],[0,0,1]])) # Output: 2
print(count_islands([[0,0],[0,0]])) # Output: 0
```
Judge scores: anthropic/claude-haiku-5.5 1/10; openai/gpt-5.6-luna 3/10.
Smollm2 360M Instruct 25% score 0.25
Answer (generation ok, stop)
```python
def count_islands(grid):
"""
Counts islands in a grid of 0s (water) and 1s (land),
where an island is a maximal group of 1s connected
in a 4-directionally (up/down/left/right) manner.
Args:
grid (list): A list of 0s and 1s representing a grid.
Returns:
int: The number of islands.
"""
def count_islands_helper(grid, row, col, count):
if not grid[row][col]:
return count
if row < 0 or col < 0 or row >= len(grid) or col >= len(grid[0]) or grid[row][col] != 1:
return count
if row == 0 or col == 0 or row == len(grid) - 1 or col == len(grid[0]) - 1:
return count + 1
count_islands_helper(grid, row + 1, col, count + 1)
count_islands_helper(grid, row - 1, col, count + 1)
count_islands_helper(grid, row, col + 1, count + 1)
count_islands_helper(grid, row, col - 1, count + 1)
return count
return count_islands_helper(grid, 0, 0, 0)
```
Judge scores: anthropic/claude-haiku-5.5 2/10; openai/gpt-5.6-luna 3/10.
Solar 10.7B Instruct V1 25% score 0.25
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
rows, cols = len(grid), len(grid[0])
visited = [[0 for _ in range(cols)] for _ in range(rows)]
islands = 0
def dfs(i, j, grid, visited):
if not (0 <= i < rows and 0 <= j < cols):
return
if visited[i][j]:
return
if grid[i][j] != 1:
return
visited[i][j] = 1
dfs(i - 1, j, grid, visited)
dfs(i + 1, j, grid, visited)
dfs(i, j - 1, grid, visited)
dfs(i, j + 1, grid, visited)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
dfs(i, j, grid, visited)
islands += 1
return islands
Judge scores: anthropic/claude-haiku-5.5 2/10; openai/gpt-5.6-luna 3/10.
Stablelm2 1.6B Chat 25% score 0.25
Answer (generation ok, stop)
Here is a Python function that counts the number of islands in a grid of 0s and 1s:
```python
def count_islands(grid):
if not grid or not grid[0]:
return 0
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '1':
count += 1
dfs(grid, i, j, count)
return count
def dfs(grid, i, j, count):
if i<0 or i>=len(grid) or j<0 or j>=len(grid[0]) or grid[i][j] != '1':
return
grid[i][j] = '0'
count_islands(grid, i+1, j, count)
count_islands(grid, i-1, j, count)
count_islands(grid, i, j+1, count)
count_islands(grid, i, j-1, count)
```
This function works by iterating over each cell in the grid. If it finds a cell with a '1', it starts a depth-first search (dfs) from that cell. The dfs function marks the current cell as '0' and recursively calls itself on the neighboring cells (up, down, left, and right). The dfs function also checks if the current cell is within the grid boundaries and if it is not a land cell (since we only want to count land cells). If the current cell is a land cell, it marks it as '0' and calls the dfs function on its neighboring cells. The dfs function continues to call itself until it reaches a land cell or a cell that is not within the grid boundaries. At the end of the dfs function, all land cells connected to the current cell are considered part of the same island. The count of islands is incremented each time the dfs function is called.
Judge scores: anthropic/claude-haiku-5.5 2/10; openai/gpt-5.6-luna 3/10.
Codellama 7B Instruct 13% score 0.13
Answer (generation ok, stop)
def count_islands(grid: list) -> int:
rows = len(grid)
cols = len(grid[0])
visited = set()
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and (i, j) not in visited:
dfs(grid, i, j, visited)
count += 1
return count
Judge scores: anthropic/claude-haiku-5.5 1/10; openai/gpt-5.6-luna 3/10.
Olmo2 7B 1124 Instruct 13% score 0.13
Answer (generation ok, stop)
```python
def count_islands(grid):
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
count = 0
def dfs(i, j):
if visited[i][j]:
return
visited[i][j] = True
count += 1
if 0 <= i < rows and 0 <= j < cols:
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
dfs(i, j)
return count
```
Judge scores: anthropic/claude-haiku-5.5 2/10; openai/gpt-5.6-luna 3/10.
Qwen3 0.6B 13% score 0.13
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
rows = len(grid)
if rows == 0:
return 0
cols = len(grid[0])
visited = [[False for _ in range(cols)] for _ in range(rows)]
def dfs(i, j):
if i < 0 or i >= rows or j < 0 or j >= cols:
return
if visited[i][j]:
return
visited[i][j] = True
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
for dx, dy in directions:
ni, nj = i + dx, j + dy
if 0 <= ni < rows and 0 <= nj < cols and not visited[ni][nj]:
dfs(ni, nj)
for i in range(rows):
for j in range(cols):
if not visited[i][j] and grid[i][j] == 1:
dfs(i, j)
return sum(1 for i in range(rows) for j in range(cols) if not visited[i][j])
```
Judge scores: anthropic/claude-haiku-5.5 1/10; openai/gpt-5.6-luna 3/10.
Codegemma 2B Code V1.1 fail score 0
Answer (generation ok, stop)
"""
def count_islands(grid):
"""
Count the number of islands in a grid of 0s (water) and 1s (land).
Args:
grid (list): A 2D grid of 0s (water) and 1s (land).
Returns:
int: The number of islands in the grid.
"""
# Initialize a counter for the number of islands
num_islands = 0
# Iterate through each cell in the grid
for i in range(len(grid)):
for j in range(len(grid[0])):
# If the current cell is a land (1), perform a DFS to explore the island
if grid[i][j] == 1:
num_islands += 1
dfs(grid, i, j)
return num_islands
def dfs(grid, i, j):
"""
Perform a depth-first search (DFS) to explore the island.
Args:
grid (list): The 2D grid of 0s (water) and 1s (land).
i (int): The row index of the current cell.
j (int): The column index of the current cell.
"""
# Base case: if the current cell is out of bounds or not a land, return
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return
# Mark the current cell as visited (set it to 0)
grid[i][j] = 0
# Recursively explore neighboring cells
dfs(grid, i+1, j) # Down
dfs(grid, i-1, j) # Up
dfs(grid, i, j+1) # Right
dfs(grid, i, j-1) # Left
# Example usage
grid = [[1, 1, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 1]]
result = count_islands(grid)
print(result) # Output: 3
"""
The code defines a function `count_islands` that takes a 2D grid of 0s (water) and 1s (land) as input.
It uses a depth-first search (DFS) algorithm to count the number of islands in the grid.
The function uses a helper function `dfs` to perform the DFS traversal.
It marks each land cell as visited by setting it to 0 and explores neighboring cells.
The function returns the number of islands found in the grid.
The example usage demonstrates the function with a sample grid and prints
Cut here for length. The model page has the full answer.
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
Codellama 13B Instruct fail score 0
Answer (generation ok, stop)
[PYTHON]
def count_islands(grid):
rows = len(grid)
cols = len(grid[0])
visited = [[False for _ in range(cols)] for _ in range(rows)]
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1 and not visited[i][j]:
dfs(grid, i, j, visited)
count += 1
return count
def dfs(grid, i, j, visited):
if i < 0 or j < 0 or i == len(grid) or j == len(grid[0]):
return
if grid[i][j] == 0 or visited[i][j]:
return
visited[i][j] = True
dfs(grid, i-1, j, visited)
dfs(grid, i+1, j, visited)
dfs(grid, i, j-1, visited)
dfs(grid, i, j+1, visited)
[/PYTHON]
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
Codellama 34B Instruct fail score 0
Answer (generation ok, stop)
[PYTHON]
def count_islands(grid: list) -> int:
def neighbors(i, j):
return [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]
def dfs(i, j):
if (i, j) in visited:
return 0
visited.add((i, j))
if grid[i][j] == "1":
for (ni, nj) in neighbors(i, j):
dfs(ni, nj)
return 1
return 0
visited = set()
islands = 0
for i in range(len(grid)):
for j in range(len(grid[i])):
islands += dfs(i, j)
return islands
[/PYTHON]
Judge scores: anthropic/claude-haiku-5.5 1/10; openai/gpt-5.6-luna 0/10.
Deepcoder 1.5B Preview fail score 0
Answer (generation truncated, length)
(empty output)
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
Deepscaler 1.5B Preview fail score 0
Answer (generation truncated, length)
(empty output)
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
Deepseek R1 1.5B Qwen Distill fail score 0
Answer (generation truncated, length)
(empty output)
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
DeepSeek-R1-Distill-Qwen-32B fail score 0
Answer (generation truncated, length)
(empty output)
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
Dolphin Phi 2.7B V2.6 fail score 0
Answer (generation ok, stop)
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i+1, j)
dfs(i-1, j)
dfs(i, j+1)
dfs(i, j-1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
print(count_islands([[1,1,0],[0,1,0],[0,0,1]]))
print(count_islands([[0,0],[0,0]]))
# Output: 2
# 0
The function `count_islands` takes a grid of 0s and 1s as input. It uses a depth-first search (DFS) to traverse the grid and mark all connected 1s as 0s. For each 1 it encounters, it starts a DFS and counts the number of islands. The final count is returned as the result.
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
Falcon2 11B fail score 0 not ranked
Answer (generation ok, stop)
def count_islands(grid):
count = 0
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 1:
count += 1
if i > 0:
if grid[i-1][j] == 1:
count += 1
if j > 0:
if grid[i-1][j-1] == 1:
count += 1
if j < len(grid[i])):
if grid[i-1][j+1] == 1:
count += 1
if i < len(grid):
if grid[i+1][j] == 1:
count += 1
if j > 0:
if grid[i+1][j-1] == 1:
count += 1
if j < len(grid[i])):
if grid[i+1][j+1] == 1:
count += 1
return count
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
Falcon3 1B Instruct fail score 0
Answer (generation ok, stop)
def count_islands(grid: list) -> int:
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
island_count = 0
def dfs(x, y):
if visited[x][y] and not visited[x][y] or (x == 0 and y == 0) or (x == rows - 1 and y == cols - 1):
island_count += 1
return
visited[x][y] = True
for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols and grid[nx][ny] == 1:
dfs(nx, ny)
dfs(0, 0)
return island_count
# Example usage:
print(count_islands([[1,1,0],[0,1,0],[0,0,1]])) # Output: 2
print(count_islands([[0,0],[0,0]])) # Output: 0
Judge scores: anthropic/claude-haiku-5.5 1/10; openai/gpt-5.6-luna 0/10.
Hermes3 3B Llama3.2 fail score 0
Answer (generation ok, stop)
def count_islands(grid:
def count_islands_grid(grid):
1
2
3
def connect_neighbours(grid, r, c):
hor = [(0, 1), (0, -1)]
ver = [(1, 0), (-1, 0)]
neighbours = [(r + r0, c + c1) for r0, c1 in zip(hor, ver) for r, c in
[(r, c), (r + r0, c + c1)] if grid[r + r0][c + c1] == 1]
return neighbours
4 5
6
return any(grid[r][c] == 1 and any(connect_neighbour(grid, r, c)) for r in range(len(grid))
for c in range(len(grid[0])))
7
8
9
10
return count_islands_grid(grid)
11
12
13
14
15
)
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
Llama2 13B Chat fail score 0
Answer (generation ok, stop)
Sure! Here is the function you requested:
def count_islands(grid: list) -> int:
m = len(grid)
n = len(grid[0])
islands = 0
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
# mark the current cell as part of an island
grid[i][j] = 2
# recurse to count the islands in the 4 directions
islands += count_islands(grid, i, j)
# undo the marking
grid[i][j] = 0
return islands
This function uses a recursive approach to count the islands. It first initializes two variables, `m` and `n`, to the length of the grid in each dimension. It then iterates over the grid, using the `for` loop, and checks each cell to see if it is a `1` (land). If it is, it marks the cell as part of an island by setting its value to `2`. It then recursively calls itself to count the islands in the 4 directions (up, down, left, and right) from the current cell. Finally, it undoes the marking by setting the cell's value back to `0`. The function returns the total number of islands found.
Note that the function takes a list of lists as input, where each sublist represents a row of the grid. It also takes two additional arguments, `i` and `j`, which represent the current cell being processed. These arguments are used in the recursive calls to specify the starting point for each direction.
Judge scores: anthropic/claude-haiku-5.5 1/10; openai/gpt-5.6-luna 0/10.
Magistral Small (2506) fail score 0
Answer (generation truncated, length)
(empty output)
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
Phi4 Mini Reasoning 3.8B fail score 0
Answer (generation truncated, length)
<think> Okay, I need to write a Python function called count_islands that takes a grid as input and returns the number of islands. The grid is made up of 0s and 1s, where 1s form islands. An island is a group of 1s connected 4-directionally. So, the task is to count how many such separate groups there are in the grid. Let me think about the approach. This sounds like a classic problem, similar to the "number of islands" LeetCode problem. The standard approach for this is using either Depth-First Search (DFS) or Breadth-First Search (BFS), or even a stack-based approach. Also, there's the method of iterating through each cell and checking its neighbors to mark the entire island at once. Let me outline the steps. For each cell in the grid that is a 1 and hasn't been counted yet, we initiate a search to mark all connected 1s as visited. Each time we start a new search, we increment the count by 1. So the main idea is to track visited cells to avoid counting the same island multiple times. Since Python doesn't modify the original grid in-place here (unless we do it), we need to create a visited structure. But modifying the grid might not be allowed if the function is supposed to work on the original grid. Wait, the problem statement says the function takes the grid as input. So if we modify the grid, we might change the input. But in the examples given, like the first example, the grid is modified during processing. However, in the second example, all are 0s, so no problem. Alternatively, we can create a visited matrix of the same size as the grid, and mark cells as visited when we process them. But modifying the grid in-place might be more efficient. Let's think about both approaches. Option 1: Modify the grid in-place. When we find a 1, we change it to 0 and perform DFS/BFS to mark all connected 1s as 0. This way, we don't need a separate visited matrix. This approach is efficient in terms of space, as we don't create a new structure. But we have to be careful n
Cut here for length. The model page has the full answer.
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 1/10.
Qwen 14B Chat V1.5 fail score 0
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
if not grid:
return 0
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)}]
count = 0
visited = set()
def is_valid(x, y):
return 0 <= x < len(grid) and 0 <= y < len(grid[x])
def dfs(x, y):
visited.add((x, y)))
grid[x][y] = 0
for dx, dy in directions:
nx, ny = x + dx, y + dy
if is_valid(nx, ny) and (nx, ny) not in visited:
dfs(nx, ny)
for i in range(len(grid)):
for j in range(len(grid[i]))):
if grid[i][j] == 1 and (i, j) not in visited:
dfs(i, j)
count += 1
return count
```
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
Qwen 4B Chat V1.5 fail score 0
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
islands = set()
for i in range(len(grid))):
for j in range(len(grid[i])))):
if grid[i][j]] == 1:
islands.add((i, j))))
return len(islands)
```
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
Qwen 7B Chat V1.5 fail score 0
Answer (generation ok, stop)
```python
def count_islands(grid):
islands = 0
for i, row in enumerate(grid):
for j, cell in enumerate(row):
if cell == 1:
island_count = 1
# Check 4-directional connectivity
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)])
for di, dj in directions:
if i + di >= 0 and i + di < len(grid) and j + dj >= 0 and j + dj < len(grid[row[i + di, j + dj]]]])):
island_count += 1
if island_count > islands:
islands = island_count
islands += island_count
return islands
```
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
Qwen2 0.5B Instruct fail score 0
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
"""
Counts the number of islands in a grid of 0s and 1s.
An island is a maximal group of 1s connected 4-directionally.
"""
rows, cols = len(grid), len(grid[0])
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
count += 1
if i > 0:
count += count_islands(grid[i-1])
if j > 0:
count += count_islands(grid[i][j-1])
return count
# Check function to verify the correctness of the solution
def check():
assert count_islands([[1,1,0],[0,1,0],[0,0,1]]) == 2
assert count_islands([[0,0],[0,0]]) == 0
assert count_islands([[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]) == 5
print("All tests passed!")
check()
```
This Python code snippet defines a function `count_islands` that counts the number of islands in a grid of 0s and 1s, where an island is a maximal group of 1s connected 4-directionally. It iterates through the grid, counting the number of islands and updating the count accordingly. The `check` function is used to verify the correctness of the solution with provided data points.
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
Qwen3 VL 2B Thinking fail score 0
Answer (generation truncated, length)
(empty output)
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
Qwen3-4B-Thinking-2507 fail score 0
Answer (generation truncated, length)
(empty output)
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
Sailor2 8B Chat fail score 0
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(cell, visited, grid, n, m):
if not (0 <= cell[0] < n and 0 <= cell[1] < m and grid[cell[0]][cell[1]] == 1): return
visited.add(cell)
grid[cell[0]][cell[1]] = 'X' # Mark visited
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] # Up, Right, Down, Left
for dx, dy in directions:
new_cell = (cell[0] + dx, cell[1] + dy)
dfs(new_cell, visited, grid, n, m)
n, m = len(grid), len(grid[0])
visited = set()
count = 0
for i in range(n):
for j in range(m):
if grid[i][j] == 1 and (i, j) not in visited:
count += 1
dfs((i, j), visited, grid, n, m)
return count
# Examples validation
print(count_islands([[1, 1, 0], [0, 1, 0], [0, 0, 1]]) -> 2)
print(count_islands([[0, 0], [0, 0]]) -> 0)
```
```
Judge scores: anthropic/claude-haiku-5.5 1/10; openai/gpt-5.6-luna 0/10.
Stable Code 3B Instruct fail score 0
Answer (generation truncated, length)
Here is a Python code block containing the function `count_islands`:
```python
def count_islands(grid):
if not grid:
return 0
rows, cols = len(grid), len(grid[0]))
visited = [[False] * cols for _ in range(rows)]
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j]] == 1 and not visited[i][j]):
count += 1
_explore_island(grid, visited, i, j)
return count
def _explore_island(grid, visited, row, col):
if (row < 0 or row >= len(grid)) or (col < 0 or col >= len(grid[0])) or visited[row][col] or grid[row][col]] == 0:
return
visited[row][col] = True
_explore_island(grid, visited, row + 1, col)
_explore_island(grid, visited, row - 1, col)
_explore_island(grid, visited, row, col + 1)
_explore_island(grid, visited, row, col - 1)
```
This code block defines the function `count_islands` which takes a 2D grid as input and returns the count of islands in the grid.
The function uses a depth-first search (DFS) algorithm to explore the islands in the grid. The algorithm marks each visited cell in the grid as visited.
The function also uses a helper function `_explore_island` to recursively explore the neighboring cells in the grid.
The function returns the count of islands in the grid.
The function also handles the edge cases where the grid is empty or contains only water cells. In these cases, the function returns 0.
The function uses a 4-directional (up/down/left/right only — NOT diagonally) approach to explore the islands in the grid.
The function uses a 2D grid as input and returns the count of islands in the grid.
The function also handles the edge cases where the grid is empty or contains only water cells. In these cases, the function returns 0.
The function uses a 4-directional (up/down/left/right only — NOT diagonally) approach to explore the islands in the grid.
The function uses a 2D grid as input and returns the count of isl
Cut here for length. The model page has the full answer.
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
YI 6B Chat V1.5 fail score 0
Answer (generation ok, stop)
from collections import deque
def count_islands(grid: list) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
islands = 0
def bfs(r, c):
q = deque()
q.append((r, c))
while q:
r, c = q.popleft()
for dr, dc in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
nr, nc = r + dr, c + dc
if (0 <= nr < rows and 0 <= nc < cols and
grid[nr][nc] == '1'):
grid[nr][nc] = '0'
q.append((nr, nc))
for r in range(rows):
for c in range(cols):
if grid[r][c] == '1':
bfs(r, c)
islands += 1
return islands
</|im_start|>
Judge scores: anthropic/claude-haiku-5.5 0/10; openai/gpt-5.6-luna 0/10.
Apertus-8B-Instruct-2509 withheld not counted
Withheld: the model stopped after one token on 212 of 270 prompts in our Ollama import, which points to a broken chat template rather than the model. It was re-run with a chat template written from the official one, on the Vulkan backend; those results replace these. This answer is shown as recorded; it has no verdict here and is not counted above.
Answer (generation ok, stop)
def count_islands(grid: list) -> int:
islands = 0
rows, cols = len(grid), len(grid[0])
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
queue = [(r, c)]
while queue:
x, y = queue.pop(0)
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols and grid[nx][ny] == 1:
grid[nx][ny] = 0
queue.append((nx, ny))
islands += 1
return islands
Falcon3 10B Instruct withheld not counted
Withheld: Ollama's library chat template for Falcon3 leaves out the newline after the last message, so the model sometimes wrote <|assistant|> itself and its reply came back empty (16 of 270 answers here; a serving bug, not the model). It was re-run with the corrected template; those results replace these. This answer is shown as recorded; it has no verdict here and is not counted above.
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] == 1:
grid[i][j] = 0
dfs(i+1, j)
dfs(i-1, j)
dfs(i, j+1)
dfs(i, j-1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Falcon3 1B Instruct withheld not counted
Withheld: Ollama's library chat template for Falcon3 leaves out the newline after the last message, so the model sometimes wrote <|assistant|> itself and its reply came back empty (42 of 270 answers here; a serving bug, not the model). It was re-run with the corrected template; those results replace these. This answer is shown as recorded; it has no verdict here and is not counted above.
Answer (generation ok, stop)
def count_islands(grid: list) -> int:
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
island_count = 0
def dfs(x, y):
if visited[x][y]:
return
visited[x][y] = True
visited[x+1][y] = visited[x][y] = False
visited[x-1][y] = visited[x][y] = False
visited[x][y+1] = visited[x][y] = False
visited[x][y-1] = visited[x][y] = False
island_count += 1
dfs(x+1, y)
dfs(x-1, y)
dfs(x, y+1)
dfs(x, y-1)
dfs(0, 0)
return island_count
Falcon3 3B Instruct withheld not counted
Withheld: Ollama's library chat template for Falcon3 leaves out the newline after the last message, so the model sometimes wrote <|assistant|> itself and its reply came back empty (169 of 270 answers here; a serving bug, not the model). It was re-run with the corrected template; those results replace these. This answer is shown as recorded; it has no verdict here and is not counted above.
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] == 0:
return 0
grid[i][j] = 0
return 1 + dfs(i + 1, j) + dfs(i - 1, j) + dfs(i, j + 1) + dfs(i, j - 1)
return sum(dfs(i, j) for i in range(len(grid)) for j in range(len(grid[0])))
```
Falcon3 7B Instruct withheld not counted
Withheld: Ollama's library chat template for Falcon3 leaves out the newline after the last message, so the model sometimes wrote <|assistant|> itself and its reply came back empty (6 of 270 answers here; a serving bug, not the model). It was re-run with the corrected template; those results replace these. This answer is shown as recorded; it has no verdict here and is not counted above.
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
Internlm2 1.8B Chat V2.5 withheld not counted
Withheld: Ollama's library chat template for InternLM2 ends the prompt without the newline after <|im_start|>assistant, so on 31 of 270 prompts the model ended its turn after one token and its reply came back empty (a serving bug, not the model). It was re-run with the corrected template; those results replace these. This answer is shown as recorded; it has no verdict here and is not counted above.
Answer (generation ok, stop)
```python
def count_islands(grid):
def dfs(grid, i, j):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != 1:
return
grid[i][j] = 0
dfs(grid, i + 1, j)
dfs(grid, i - 1, j)
dfs(grid, i, j + 1)
dfs(grid, i, j - 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(grid, i, j)
count += 1
return count
```
Qwen2 Math 1.5B Instruct withheld not counted
Withheld: Ollama's library chat template for Qwen2-Math differs from the official one (no newline between turns or after the final <|im_start|>assistant, and no default system turn). It was re-run with the official template; those results replace these (the scores changed very little). This answer is shown as recorded; it has no verdict here and is not counted above.
Answer (generation ok, stop)
To solve the problem of counting the number of islands in a grid, we can use a depth-first search (DFS) algorithm. Here's a step-by-step explanation of the solution:
1. **Define the Function**: We need to define a function `count_islands` that takes a grid as input and returns the number of islands.
2. **Check for Island Validity**: We need to check if a cell is part of an island. A cell is part of an island if it is a `1` (land) and has not been visited yet.
3. **Perform DFS**: We will perform a DFS starting from each cell that is a `1` and mark all connected `1`s as visited. Each time we start a new DFS from an unvisited `1`, we count it as one island.
4. **Iterate Over the Grid**: We iterate over each cell in the grid. If a cell is a `1`, we start a DFS from that cell and increment the island count by 1.
Here's the Python code that implements this:
```python
def count_islands(grid):
count = 0
rows, columns = len(grid), len(grid[0])
def DFS(i, j):
if i < rows and j < columns and grid[i][j] == 1 and not visited[i][j]:
visited[i][j] = True
DFS(i-1, j) # up
DFS(i+1, j) # down
DFS(i, j-1) # left
DFS(i, j+1) # right
for i in range(rows):
for j in range(columns):
if grid[i][j] == 1 and not visited[i][j]:
visited[i][j] = True
DFS(i, j)
for row in visited:
for cell in row:
if cell == True:
count += 1
return count
def DFS(i, j):
if i < rows and j < columns and grid[i][j] == 1 and not visited[i][j]:
visited[i][j] = True
DFS(i-1, j) # up
DFS(i+1, j) # down
DFS(i, j-1) # left
DFS(i, j+1) # right
def initialize visited:
visited = [[False for _ in range(columns)] for _ in range(rows))
```
The `count_islands` function iterates over each cell in the grid. If it finds a `1` that has not been visited, it starts a DFS from tha
Cut here for length. The model page has the full answer.
Qwen2 Math 7B Instruct withheld not counted
Withheld: Ollama's library chat template for Qwen2-Math differs from the official one (no newline between turns or after the final <|im_start|>assistant, and no default system turn). It was re-run with the official template; those results replace these (the scores changed very little). This answer is shown as recorded; it has no verdict here and is not counted above.
Answer (generation ok, stop)
```python
def count_islands(grid: list) -> int:
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i+1, j)
dfs(i-1, j)
dfs(i, j+1)
dfs(i, j-1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
dfs(i, j)
count += 1
return count
```
The same data as JSON: coding-010.json. Compare two models to see every question where they differ.