https://www.acmicpc.net/problem/10026
n = int(input())
arr = [list(input()) for _ in range(n)]
arr2 = [arr[i][:] for i in range(n)]
color = ['R','G','B']
dy = [0,1,0,-1]
dx = [1,0,-1,0]
# 적록색약 ver arr2
for i in range(n):
for j in range(n):
if arr[i][j] == 'R':
arr2[i][j] = 'G'
def dfs(arr,y,x,c):
q = []
arr[y][x] = '0'
q.append((y,x))
while(q):
a,b = q.pop()
for i in range(4):
ny, nx = a + dy[i], b + dx[i]
if 0 <= ny < n and 0 <= nx < n and arr[ny][nx] == c:
arr[ny][nx] = '0'
q.append((ny,nx))
cnt = 0
for i in range(n):
for j in range(n):
if arr[i][j] != '0':
dfs(arr,i,j,arr[i][j])
cnt += 1
cnt2 = 0
for i in range(n):
for j in range(n):
if arr2[i][j] != '0':
dfs(arr2,i,j,arr2[i][j])
cnt2 += 1
print(cnt,end=' ')
print(cnt2)
그냥 평범한 dfs