def solution(m, n, board):
answer = 0
dx = [1,1,0]
dy = [1,0,1]
def cnt_one(m,n,visit):
cnt = 0
for i in range(m):
for j in range(n):
if visit[i][j] ==1:
cnt +=1
return cnt
while True:
'''
1) 사각형이 확인되면 그때의 좌표에 visit 1표시하기
2) visit이 1인 갯수 카운트해서 answer에 더하기
3) board 새거로 업데이트
'''
visit = [[0 for col in range(n)] for row in range(m)]
# 1)사각형에 visit 1 표시하기
for i in range(m):
for j in range(n):
item = board[i][j]
cnt = 0
if item == 0:
continue
#사각형인지 확인
for k in range(3):
nx = i + dx[k]
ny = j + dy[k]
if nx<0 or nx>=m or ny<0 or ny>=n:
continue
if board[nx][ny] == item:
cnt += 1
#사각형이면 visit에 표시하기
if cnt == 3:
#print(i,j)
visit[i][j]=1
for k in range(3):
nx = i + dx[k]
ny = j + dy[k]
if visit[nx][ny] == 0:
visit[nx][ny] = 1
del_cnt = cnt_one(m,n,visit)
answer += del_cnt
if del_cnt == 0:
break
new_board=[]
# 3)board 업데이트
for i in range(n):
stk = []
for j in range(m):
if visit[j][i] == 0:
stk.append(board[j][i])
stk = stk[::-1]
if len(stk)!=m:
stk=stk+[0]*(m-len(stk))
stk = stk[::-1]
new_board.append(stk)
board = list(map(list, zip(*new_board)))
return answer