정답 코드
from collections import deque
n, m = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(n)]
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]
visited = [[False] * m for _ in range(n)]
def bfs(x, y):
queue = deque([(x, y)])
visited[x][y] = True
pic = 1
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m:
if not visited[nx][ny] and graph[nx][ny] == 1:
visited[nx][ny] = True
queue.append((nx, ny))
pic += 1
return pic
count = 0
max_pic = 0
for i in range(n):
for j in range(m):
if not visited[i][j] and graph[i][j] == 1:
pic = bfs(i, j)
count += 1
max_pic = max(max_pic, pic)
print(count)
print(max_pic)