BFS : Breadth First Search. (너비우선)
DFS : Depth First Search. (깊이우선)

사용하는 자료구조 :
import sys
from collections import deque
input = sys.stdin.readline
n, m = map(int, input().split())
array = [list(map(int, input().split())) for _ in range(n)]
visited = [[False] * m for _ in range(n)]
# 기본 자료구조 세팅
direction_y = [0, 1, 0, -1]
direction_x = [1, 0, -1, 0]
def bfs(y, x):
result = 1
queue = deque([(y, x)])
while queue:
now_y, now_x = queue.popleft()
for k in range(4):
next_y = now_y + direction_y[k]
next_x = now_x + direction_x[k]
if 0 <= next_y < n and 0 <= next_x < m:
if array[next_y][next_x] == 1 and visited[next_y][next_x] == False:
result += 1
visited[next_y][next_x] = True
queue.append((next_y, next_x))
return result
count = 0
max_v = 0
for j in range(n):
for i in range(m):
if array[j][i] == 1 and visited[j][i] == False:
visited[j][i] = True
count += 1
max_v = max(max_v, bfs(j, i))
print(count)
print(max_v)
import sys
input = sys.stdin.readline
n = int(input().rstrip())
array = [list(map(int, input().strip())) for _ in range(n)]
visited = [[False] * n for _ in range(n)]
result = []
separate = 0
direction_x = [1,0,-1,0]
direction_y = [0,1,0,-1]
def dfs(x, y):
global separate
separate += 1
for k in range(4):
next_y = y + direction_y[k]
next_x = x + direction_x[k]
if 0 <= next_x < n and 0 <= next_y < n:
if array[next_x][next_y] == 1 and visited[next_x][next_y] == False:
visited[next_x][next_y] = True
dfs(next_x, next_y)
for i in range(n):
for j in range(n):
if array[i][j] == 1 and visited[i][j] == False:
visited[i][j] = True
separate = 0
dfs(i,j)
result.append(separate)
result.sort()
print(len(result))
for e in result:
print(e)