def answer(block):
rows,cols = len(block),len(block[0])
directions = [(0,1),(1,0),(0,-1),(-1,0)]
direction = 0
x,y = 0,0
count = 0
visited= [[False]*cols for _ in range(rows)]
while not visited[x][y]:
visited[x][y]= True
count +=1
fine_next= False
for _ in range(4):
new_x,new_y = x+directions[direction][0],y+directions[direction][1]
if 0<=new_x <rows and 0<=new_y <cols and block[new_x][new_y]==0:
x , y = new_x , new_y
find_next=True
if find_next:
break
direction = (direction+1)%4
if not find_next:
break
return count