board = [
'##..##',
'#.###.',
'###.#.',
'...###']
I = len(board)
J = len(board[0])
room_no = 0
def get_range(i:int, j:int):
for ii in range(i - 1, i + 2):
for jj in range(j - 1, j + 2):
if ii < 0: continue
if ii >= I: continue
if jj < 0: continue
if jj >= J: continue
yield ii, jj
def search(x):
cnt = 0
i, j = x
if board[i][j] == '.':
cnt += 1
board[i][j] = str(room_no)
for x in get_range(i, j):
search(x)
return cnt
def pp():
for i in board:
print(''.join(i))
board = list(map(list, board))
for i in range(I):
for j in range(J):
if search((i, j)) > 0:
room_no += 1
pp()
print(room_no, 'rooms')
Ym9hcmQgPSBbCiAgICAnIyMuLiMjJywKICAgICcjLiMjIy4nLAogICAgJyMjIy4jLicsCiAgICAnLi4uIyMjJ10KCkkgPSBsZW4oYm9hcmQpCkogPSBsZW4oYm9hcmRbMF0pCnJvb21fbm8gPSAwCgoKZGVmIGdldF9yYW5nZShpOmludCwgajppbnQpOgogICAgZm9yIGlpIGluIHJhbmdlKGkgLSAxLCBpICsgMik6CiAgICAgICAgZm9yIGpqIGluIHJhbmdlKGogLSAxLCBqICsgMik6CiAgICAgICAgICAgIGlmIGlpIDwgMDogY29udGludWUKICAgICAgICAgICAgaWYgaWkgPj0gSTogY29udGludWUKICAgICAgICAgICAgaWYgamogPCAwOiBjb250aW51ZQogICAgICAgICAgICBpZiBqaiA+PSBKOiBjb250aW51ZQogICAgICAgICAgICB5aWVsZCBpaSwgamoKCgpkZWYgc2VhcmNoKHgpOgogICAgY250ID0gMAogICAgaSwgaiA9IHgKICAgIGlmIGJvYXJkW2ldW2pdID09ICcuJzoKICAgICAgICBjbnQgKz0gMQogICAgICAgIGJvYXJkW2ldW2pdID0gc3RyKHJvb21fbm8pCiAgICAgICAgZm9yIHggaW4gZ2V0X3JhbmdlKGksIGopOgogICAgICAgICAgICBzZWFyY2goeCkKICAgIHJldHVybiBjbnQKCgpkZWYgcHAoKToKICAgIGZvciBpIGluIGJvYXJkOgogICAgICAgIHByaW50KCcnLmpvaW4oaSkpCgoKYm9hcmQgPSBsaXN0KG1hcChsaXN0LCBib2FyZCkpCmZvciBpIGluIHJhbmdlKEkpOgogICAgZm9yIGogaW4gcmFuZ2UoSik6CiAgICAgICAgaWYgc2VhcmNoKChpLCBqKSkgPiAwOgogICAgICAgICAgICByb29tX25vICs9IDEKcHAoKQpwcmludChyb29tX25vLCAncm9vbXMnKQo=