Title: Island Count

There is a 12x12 grid representing the topography. Each cell is either black (land) or white (sea). Two black cells are considered connected if they are adjacent vertically or horizontally. A single black cell or a group of connected black cells form an "island". The task is to create a program that reads the grid data and outputs the number of islands for each dataset.

Input:
Multiple datasets are provided. Each dataset consists of a 12x12 grid represented by 12 rows of 12 numbers (0 for white, 1 for black). Datasets are separated by a single empty line. The number of datasets does not exceed 20.

Output:
For each dataset, output the number of islands in a single line.

Sample Input:
[12x12 grid data]

Sample Output:
[Number of islands for each dataset]

Hint:
The sample input is represented using ■ and □.

# Buggy version:
import itertools

def getPoint(m,x,y):
    if not (0 <= x < 12) or not (0 <= y < 12):
        return 0
    return m[y][x]

def setPoint(m,x,y,v):
    m[y][x] = v

while 1:
    try:
        m = [list(map(int,input().strip())) for _ in range(12)]
        count = 0
        empty = False
        while not empty:
            empty = True
            for x,y in itertools.product(range(12),repeat=2):
                if(getPoint(m,x,y)):
                    print(x,y)
                    count += 1
                    empty = False
                    q = []
                    q.append((x,y))
                    while q != []:
                        p = q.pop()
                        if(getPoint(m,p[0],p[1])):
                            setPoint(m,p[0],p[1],0)
                            for o in [(1,0),(-1,0),(0,1),(0,-1)]:
                                q.append((p[0] + o[0], p[1] + o[1]))
        print(count)
        input()
    except:
        exit()

# Fixed version:
import itertools

def getPoint(m,x,y):
    if not (0 <= x < 12) or not (0 <= y < 12):
        return 0
    return m[y][x]

def setPoint(m,x,y,v):
    m[y][x] = v

while 1:
    try:
        m = [list(map(int,input().strip())) for _ in range(12)]
        count = 0
        empty = False
        while not empty:
            empty = True
            for x,y in itertools.product(range(12),repeat=2):
                if(getPoint(m,x,y)):
                    count += 1
                    empty = False
                    q = []
                    q.append((x,y))
                    while q != []:
                        p = q.pop()
                        if(getPoint(m,p[0],p[1])):
                            setPoint(m,p[0],p[1],0)
                            for o in [(1,0),(-1,0),(0,1),(0,-1)]:
                                q.append((p[0] + o[0], p[1] + o[1]))
        print(count)
        input()
    except:
        exit()

# Buggy version:
def fill(x, y, board):
    board[y][x] = 2
    points = [[y, x + 1], [y, x - 1], [y + 1, x], [y - 1, x]]
    # y
    if y == 0:
        points.remove([y-1, x])
    elif y == 11:
        points.remove([y+1, x])
    # x
    if x == 0:
        points.remove([y, x - 1])
    elif x == 11:
        points.remove([y, x + 1])
    for p in points:
        if board[p[0]][p[1]] == 1:
            board = fill(p[1], p[0], board)
    return board

while True:
    try:
        islands = [list(map(int, list(input()))) for i in range(12)]
        input()
        ans = 0

        for y in range(12):
            for x in range(12):
                if islands[y][x] == 1:
                    islands = fill(x, y, islands)
                    ans += 1
        print(ans)
    except:
        break

# Fixed version:
def fill(x, y, board):
    board[y][x] = 2
    points = [[y, x + 1], [y, x - 1], [y + 1, x], [y - 1, x]]

    if y == 0:
        points.remove([y-1, x])
    elif y == 11:
        points.remove([y+1, x])
    if x == 0:
        points.remove([y, x - 1])
    elif x == 11:
        points.remove([y, x + 1])

    for p in points:
        if board[p[0]][p[1]] == 1:
            board = fill(p[1], p[0], board)
    return board

while True:
    try:
        islands = [list(map(int, list(input()))) for i in range(12)]
        ans = 0

        for y in range(12):
            for x in range(12):
                if islands[y][x] == 1:
                    islands = fill(x, y, islands)
                    ans += 1
        print(ans)
        input()
    except:
        break

# Buggy version:
def f1():
    t = 0
    for y, line in enumerate(M):
        for x, cell in enumerate(line):
            if M[y][x] == '1':
                f2(x, y)
                t += 1
    return t


def f2(x, y):
    if x < 0 or len(M[0]) == x or y < 0 or len(M) == y:
        return
    if M[y][x] == '1':
        M[y][x] = '0'
        f2(x, y-1)
        f2(x, y+1)
        f2(x+1, y)
        f2(x-1, y)


M = []
while True:
    try:
        line = input()
        if line == '':
            print(f1())
            M = []
        else:
            M.append(list(input()))
    except EOFError:
        print(f1())
        break

# Fixed version:
def f1():
    t = 0
    for y in range(len(M)):
        for x in range(len(M[0])):
            if M[y][x] == '1':
                f2(x, y)
                t += 1
    return t


def f2(x, y):
    if x < 0 or len(M[0]) == x or y < 0 or len(M) == y:
        return
    if M[y][x] == '1':
        M[y][x] = '0'
        f2(x, y-1)
        f2(x, y+1)
        f2(x+1, y)
        f2(x-1, y)


M = []
while True:
    try:
        line = input()
        if line == '':
            print(f1())
            M = []
        else:
            M.append(list(line))
    except EOFError:
        print(f1())
        break

# Buggy version:
def remove_linked(x, y, DATA):
    DATA[x][y]=0# clear
    move = [[1, 0], [0, 1], [-1, 0], [0, -1]]#left, right, down, up
    for i, j in move:
        nx,ny=x+i,y+j#next x and y
        if -1<nx<12 and -1<ny<12 and DATA[nx][ny]:
            DATA=remove_linked(nx, ny, DATA)
    return DATA

DATA = [[int(x) for x in list(input())] for _ in range(12)]
count = 0
for x in range(12):
    for y in range(12):
        if DATA[x][y]:
            count += 1
            DATA = remove_linked(x, y, DATA)
print (count)

# Fixed version:
def remove_linked(x, y, DATA):
    DATA[x][y]=0# clear
    move = [[1, 0], [0, 1], [-1, 0], [0, -1]]#left, right, down, up
    for i, j in move:
        nx,ny=x+i,y+j#next x and y
        if -1<nx<12 and -1<ny<12 and DATA[nx][ny]:
            DATA=remove_linked(nx, ny, DATA)
    return DATA

while 1:
    DATA = [[int(x) for x in list(input())] for _ in range(12)]
    count = 0
    for x in range(12):
        for y in range(12):
            if DATA[x][y]:
                count += 1
                DATA = remove_linked(x, y, DATA)
    print (count)
    
    try:input()
    except:break

# Buggy version:
def counter():#島の数をカウントする
    count=0
    #12*12個の値をひとつずつ調べる
    for x in range(12):
        for y in range(12):
            if is_data[x][y] == 1:#島が発見されたら
                print(x,y)
                dfs(x,y)
                count+=1
    return count
def dfs(x,y):
    if x<0 or x == len(is_data[x]) or y<0 or y == len(is_data):
        return
    if is_data[x][y] == 1:
        is_data[x][y] = 0
        dfs(x+1,y)
        dfs(x,y+1)
        dfs(x-1,y)
        dfs(x,y-1)

try:
    is_data=[]
    while True:
        s = input()#入力
        if not s :#空入力のときループぬける
            break
        else:
            is_data.append(s)
    for i in range(len(is_data)):
        is_data[i] = list(map(int,is_data[i]))
    is_data = np.array(is_data).reshape(12,12)
except EOFError:
    print(counter())


# Fixed version:
def counter():#島の数をカウントする
    count=0
    #12*12個の値をひとつずつ調べる
    for x in range(len(is_data)):
        for y in range(len(is_data)):
            if is_data[x][y] == '1':#島が発見されたら
                dfs(x,y)
                count+=1
    return count
def dfs(x,y):
    if x<0 or x == len(is_data) or y<0 or y == len(is_data):
        return
    if is_data[x][y] == '1':
        is_data[x][y] = '0'
        dfs(x+1,y)
        dfs(x,y+1)
        dfs(x-1,y)
        dfs(x,y-1)

is_data=[]
while True:
    try:
        s = list(input())#入力
        if not s:#空入力のときループぬける
            print(counter())
            is_data=[]
            s = []
        else:
            #ls = len(s)
            is_data.append(s)
            #for i in range(len(is_data)):
                #is_data[i] = list(map(int,is_data[i]))
                #is_data = np.array(is_data).reshape(ls,ls)
#print(is_data)
    except EOFError:
        print(counter())
        break

# Buggy version:
def counter():#島の数をカウントする
    count=0
    #12*12個の値をひとつずつ調べる
    for x in range(len(is_data)):
        for y in range(len(is_data)):
            if is_data[x][y] == '1':#島が発見されたら
                dfs(x,y)
                count+=1
    return count
def dfs(x,y):
    if x<0 or x == len(is_data) or y<0 or y == len(is_data):
        return
    if is_data[x][y] == '1':
        is_data[x][y] = '0'
        dfs(x+1,y)
        dfs(x,y+1)
        dfs(x-1,y)
        dfs(x,y-1)

is_data=[]
while True:
    try:
        s = list(input())#入力
        if not s:#空入力のとき
            #print(counter())
            is_data=[]
            s = []
        else:
            is_data.append(s)
    except EOFError:
        print(counter())
        break


# Fixed version:
def counter():#島の数をカウントする
    count=0
    #12*12個の値をひとつずつ調べる
    for x in range(len(is_data)):
        for y in range(len(is_data)):
            if is_data[x][y] == '1':#島が発見されたら
                dfs(x,y)
                count+=1
    return count
def dfs(x,y):
    if x<0 or x == len(is_data) or y<0 or y == len(is_data):
        return
    if is_data[x][y] == '1':
        is_data[x][y] = '0'
        dfs(x+1,y)
        dfs(x,y+1)
        dfs(x-1,y)
        dfs(x,y-1)

is_data=[]
while True:
    try:
        s = list(input())#入力
        if not s:#空入力のとき
            print(counter())
            is_data=[]
            s = []
        else:
            is_data.append(s)
    except EOFError:
        print(counter())
        break