def bfs(image, visited, y, x):
q = deque()
q.append((x,y))
while q:
(x,y) = q.popleft()
# up
if y - 1 >= 0 and not visited[y - 1][x] and image[y - 1][x] in ['L', 'C']:
q.append((x, y-1))
visited[y-1][x] = True
# down
if y + 1 < len(image) and not visited[y + 1][x] and image[y + 1][x] in ['L', 'C']:
q.append((x, y+1))
visited[y+1][x] = True
# left
if x - 1 >= 0 and not visited[y][x - 1] and image[y][x - 1] in ['L', 'C']:
q.append((x-1, y))
visited[y][x-1] = True
# right
if x + 1 < len(image[0]) and not visited[y][x + 1] and image[y][x + 1] in ['L', 'C']:
q.append((x+1, y))
visited[y][x+1] = True
// create graph here ...
// count islands
visited = [[False for i in range(c)] for j in range(r)]
count = 0
for (x, y) in lands:
if not visited[y][x]:
bfs(image, visited, y, x)
count += 1