from collections import deque
def solution(board):
answer = 0
n = len(board)
q = deque()
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [1, -1, 1, -1, 0, 0, 1, -1]
for i in range(n):
for j in range(n):
if board[i][j] == 1:
q.append((i, j))
while q:
px, py = q.popleft()
for i in range(8):
nx = px + dx[i]
ny = py + dy[i]
if nx < 0 or nx >= n or ny < 0 or ny >= n:
continue
if board[nx][ny] == 0:
board[nx][ny] = 2
for b in board:
answer += b.count(0)
return answer