Submission

Status:

[PPPPPPPPPPPPPPPPPPPPPPPPP]

Subtask/Task Score:

{100/100}

Score: 100

User: jithsmoth

Problemset: เกาะที่ใหญ่ที่สุด

Language: python

Time: 0.045 second

Submitted On: 2025-12-13 22:08:31

from collections import deque
def main():
    N, M = map(int, input().split())
    
    grid = []
    
    for i in range(N):
        row = input()
        row = row.strip()
        grid.append(list(row))
    # print(grid)
    
    visited = []
    for i in range(N):
        visited.append([False] * M)
    
    dir_x = [1, -1, 0, 0]
    dir_y = [0, 0, 1, -1]
    def bfs(start_x, start_y, grid, visited, N, M):
        q = deque()
        q.append((start_x, start_y))
        visited[start_x][start_y] = True
        counter = 0
    
        while q:
            x,y = q.popleft()
            counter += 1
            
            for dir_idx in range(4):
                next_x = x + dir_x[dir_idx]
                next_y = y + dir_y[dir_idx]
                
                if 0 <= next_x < N and 0 <= next_y < M:
                    if grid[next_x][next_y] =='1' and not visited[next_x][next_y]:
                        visited[next_x][next_y] = True
                        q.append((next_x, next_y))
        return counter                        
    
    
    result = 0
    for i in range(N):
        for j in range(M):
            
            if grid[i][j] == '1' and not visited[i][j]:
                size = bfs(i, j, grid, visited, N, M)
                if size > result:
                    result = size
    print(result)
main()