- ์
๋ ฅ๋ฐ์ n, m์ผ๋ก ์ด์ฐจ์๋ฐฐ์ด ๋ง๋ ๋ค. ๊ทธ๋ํ ํ๋์ฉ ๋๋ฉด์ BFS ๋ฅผ ์คํํ๋ค.
- ์คํํ ๋๋ง๋ค ํ ์์ญ์ด ์์๋๋ ๊ฒ์ด๋ฏ๋ก ์ต์ข
์์ญ์ด ๋ช ๊ฐ์ธ์ง ๋ํ๋ด๋ ๋ณ์ count๋ฅผ +1 ํ๋ค.
- BFS ์์์๋ ํ ์กฐ๊ฐ(?)๋ง๋ค cnt๋ฅผ +1 ํ์ฌ ํ ์์ญ ์์ ์กฐ๊ฐ์ด ๋ช ๊ฐ ์๋์ง ์ผ๋ค.
- count์ ๊ฐ ์์ญ์ cnt ์ค์ max๋ฅผ ์ถ๋ ฅํ๋ค.
๐ซ ์์ฆ ์์ฃผํ๋ ์ค์,,
- ๋ฐฉํฅ ์ขํ ์ฐ๊ณ
0 <= aa < n and 0 <= bb < m
์กฐ๊ฑด๋ฌธ ๋น ํธ๋ฆฌ์ง ๋ง๊ธฐ
- graph ๋ด์ ๋ฐฉ๋ฌธ์ฒดํฌ ํ๋ฉด ์๋๊ณ visited ๋ณ์ ์จ์ ๋ฐฉ๋ฌธ์ฒดํฌ ํ๊ธฐ
- ๋ฐฉํฅ์ฒดํฌ ์ขํ๋ฅผ ์จ์ผํ๋ ๋ฌธ์ ์ ๊ทธ๋ ์ง ์์ ๋ฌธ์ ๊ตฌ๋ถํ๊ธฐ
import sys
input = sys.stdin.readline
from collections import deque
def BFS(x, y):
q = deque()
cnt = 1
q.append([x, y])
visited[x][y] = 1
while q:
a, b = q.popleft()
for i in range(4):
aa = a + dx[i]
bb = b + dy[i]
if 0 <= aa < n and 0 <= bb < m and graph[aa][bb] == 1 and visited[aa][bb] == 0:
visited[aa][bb] = 1
q.append([aa, bb])
cnt += 1
return cnt
n, m = map(int, input().split())
graph = []
for _ in range(n):
lst = list(map(int, input().split()))
graph.append(lst)
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
count = 0
cnt_list = []
visited = [[0]*m for _ in range(n)]
for i in range(n):
for j in range(m):
if graph[i][j] == 1 and visited[i][j] == 0:
cnt_list.append(BFS(i, j))
count += 1
if count != 0:
print(count)
print(max(cnt_list))
else:
print(count)
print(0)