https://www.acmicpc.net/problem/1926
import sys
from collections import deque
n, m = map(int, input().split())
dx = [0, 0, 1, -1]
dy = [-1, 1, 0, 0]
def bfs(i, j):
cnt = 1
que = deque()
que.append((i, j))
visited[i][j] = True
while que:
x, y = que.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m and visited[nx][ny] == False:
if paper[nx][ny] == 1:
visited[nx][ny] = True
cnt += 1
que.append((nx, ny))
return cnt
paper = []
res = []
visited = [[False] * m for _ in range(n)]
for _ in range(n):
paper.append(list(map(int, input().split())))
for i in range(n):
for j in range(m):
if paper[i][j] == 1 and visited[i][j] == False:
res.append(bfs(i, j))
if res:
print(len(res))
print(max(res))
else:
print(0)
print(0)
BFS를 이용해서 풀면 된다.
BFS에서 방문한 값들을 return 값으로 받아, res에 더해준다.