๋ฐฑ์ค 2468๋ฒ ํ์ด์ฌ
import sys
import copy
from collections import deque
input = sys.stdin.readline
land = []
high = 0
n = int(input())
visited_copy = [[False] * n for _ in range(n)]
for _ in range(n):
l = list(map(int, input().split()))
high = max(max(l), high) # ์
๋ ฅ๋ ์ฌ์ ๋์ด ์ค์ ๊ฐ์ฅ ๋์ ์ฌ์ ์ฐพ์
land.append(l)
move = ((-1, 0), (1, 0), (0, -1), (0, 1)) # ์ํ์ข์ฐ ์ด๋
cnt = 0
result = []
for num in range(high+1):
visited = copy.deepcopy(visited_copy) # ๊น์ ๋ณต์ฌ
cnt = 0
for i in range(n):
for j in range(n):
if (not visited[i][j] and land[i][j] > num):
# (land[i][j] > num) ์ด ์กฐ๊ฑด์ด ์์ผ๋ฉด ์ ๊ธด ์ฌ๋ค๋ ๋ชจ๋ ํ์ํ๋ค.
q = deque([(i, j)])
visited[i][j] = True
cnt += 1
while q:
xi, xj = q.popleft()
for mi, mj in move:
ci, cj = xi + mi, xj + mj
if (0<=ci<n) and (0<=cj<n):
if (not visited[ci][cj]) and (land[ci][cj] > num):
# ์ํ์ข์ฐ ์ด๋ํ๋ฉด์ ๋ฐฉ๋ฌธํ์ ์๊ณ ๋ฒ์ ๋ด์ ์์ ๋ ์ด๋
visited[ci][cj] = True
q.append((ci, cj))
result.append(cnt)
print(max(result)) # ์ ์ผ ์ฌ์ ๊ฐฏ์๊ฐ ๋ง์ ๋ ์ถ๋ ฅ