Problem:


島の数

地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。

■■■■□□□□■■■■
■■■□□□□□■■■■
■■□□□□□□■■■■
■□□□□□□□■■■■
□□□■□□□■□□□□
□□□□□□■■■□□□
□□□□□■■■■■□□
■□□□■■■■■■■□
■■□□□■■■■■□□
■■■□□□■■■□□□
■■■■□□□■□□□□
□□□□□□□□□□□□

マスのデータを読み込んで、島の数を出力するプログラムを作成してください。

Input

入力は複数のデータセットからなります。各データセットに１つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は１つの空行で区切られています。

データセットの数は 20 を超えません。

Output

データセットごとに、島の数を１行に出力します。

Sample Input

111100001111
111000001111
110000001111
100000001111
000100010000
000000111000
000001111100
100011111110
110001111100
111000111000
111100010000
000000000000

010001111100
110010000010
010010000001
010000000001
010000000110
010000111000
010000000100
010000000010
010000000001
010010000001
010010000010
111001111100

000000000000
111111111111
100010100001
100010100001
100010100001
100010100001
100100100101
101000011101
100000000001
100000000001
111111111111
100000000001

Output for the Sample Input

5
13
4

Hint

以下はサンプルインプットを■と□で表したものです。

■■■■□□□□■■■■　　□■□□□■■■■■□□　　□□□□□□□□□□□□
■■■□□□□□■■■■　　■■□□■□□□□□■□　　■■■■■■■■■■■■
■■□□□□□□■■■■　　□■□□■□□□□□□■　　■□□□■□■□□□□■
■□□□□□□□■■■■　　□■□□□□□□□□□■　　■□□□■□■□□□□■
□□□■□□□■□□□□　　□■□□□□□□□■■□　　■□□□■□■□□□□■
□□□□□□■■■□□□　　□■□□□□■■■□□□　　■□□□■□■□□□□■
□□□□□■■■■■□□　　□■□□□□□□□■□□　　■□□■□□■□□■□■
■□□□■■■■■■■□　　□■□□□□□□□□■□　　■□■□□□□■■■□■
■■□□□■■■■■□□　　□■□□□□□□□□□■　　■□□□□□□□□□□■
■■■□□□■■■□□□　　□■□□■□□□□□□■　　■□□□□□□□□□□■
■■■■□□□■□□□□　　□■□□■□□□□□■□　　■■■■■■■■■■■■
□□□□□□□□□□□□　　■■■□□■■■■■□□　　■□□□□□□□□□□■



Sample input:
111100001111
111000001111
110000001111
100000001111
000100010000
000000111000
000001111100
100011111110
110001111100
111000111000
111100010000
000000000000

010001111100
110010000010
010010000001
010000000001
010000000110
010000111000
010000000100
010000000010
010000000001
010010000001
010010000010
111001111100

000000000000
111111111111
100010100001
100010100001
100010100001
100010100001
100100100101
101000011101
100000000001
100000000001
111111111111
100000000001


Sample output:
5
13
4

Submission status: Accepted

Solution code:
import sys
A=range(12)
m=[[0 for i in A] for j in A]
def f(y,x):
    f=0
    if m[y][x]==1:
        m[y][x]=c
        f=1
    return f
def LR(y,x,dx):
    P=[]
    x+=dx
    while 0<=x<12 and m[y][x]!=0:
        if f(y,x):P+=[(y,x)]
        x+=dx
    return P
def UD(y,x,dy):
    P=[]
    y+=dy
    while 0<=y<12 and m[y][x]!=0:
        if f(y,x):P+=[(y,x)]
        y+=dy
    return P
def v1(buf):
    f=0
    while len(buf)>0:
        f=1
        y,x=buf.pop()
        m[y][x]=c
        buf+=LR(y,x,-1)+LR(y,x,1)+UD(y,x,-1)+UD(y,x,1)
    return f
def solve():
    global c
    c=2
    for i in A:
        x=m[i]
        while x.count(1)>0:
            v1([(i,x.index(1))])
            c+=1
    return c-2

y=0
for s in sys.stdin:
    if y<12:
        x=0
        for e in map(int,list(s[:-1])):
            m[y][x]=e
            x+=1
        y+=1
    else:
        print solve()
        y=0
print solve()

Submission status: Accepted

Solution code:
import sys
A=range(12)
m=[[0 for i in A] for j in A]
def LRUD(y,x,d):
    P=[]
    x+=d[0]
    y+=d[1]
    if 0<=x<12 and 0<=y<12 and m[y][x]==1:
        m[y][x]=c
        P=[(y,x)]
    return P
def v1(buf):
    while len(buf)>0:
        y,x=buf.pop()
        m[y][x]=c
        for e in [(-1,0),(1,0),(0,-1),(0,1)]:
            buf+=LRUD(y,x,e)
    return
def solve():
    global c
    c=2
    for i in A:
        x=m[i]
        while x.count(1)>0:
            v1([(i,x.index(1))])
            c+=1
    print c-2
    return 0

y=0
for s in sys.stdin:
    if y<12:
        x=0
        for e in map(int,list(s[:-1])):
            m[y][x]=e
            x+=1
        y+=1
    else:
        y=solve()
solve()

Submission status: Accepted

Solution code:
import sys
A=range(12)
m=[[0 for i in A] for j in A]
def f(y,x,d):
    P=[]
    x+=d[0]
    y+=d[1]
    if 0<=x<12 and 0<=y<12 and m[y][x]==1:
        m[y][x]=c
        P=[[y,x]]
    return P
def v1(buf):
    global c
    while buf!=[]:
        y,x=buf.pop()
        m[y][x]=c
        for e in [(-1,0),(1,0),(0,-1),(0,1)]:buf+=f(y,x,e)
    c+=1
    return
def solve():
    global c
    c=2
    for i in A:
        while m[i].count(1)>0:v1([[i,m[i].index(1)]])
    print c-2
    return 0
y=0
for s in sys.stdin:
    if y<12:
        m[y]=map(int,list(s[:-1]))
        y+=1
    else:y=solve()
solve()

Submission status: Accepted

Solution code:
import sys
A=range(14)
m=[[0 for i in A] for j in A]
def f(y,x):
    if m[y][x]==1:
        m[y][x]=0
        f(y,x+1)
        f(y,x-1)
        f(y+1,x)
        f(y-1,x)
    return
def solve():
    c=2
    for i in A:
        while m[i].count(1)>0:
            f(i,m[i].index(1))
            c+=1
    print c-2
    return 0
y=0
for s in sys.stdin:
    if y<12:
        m[y+1]=[0]+map(int,list(s[:-1]))+[0]
        y+=1
    else:y=solve()
solve()

Submission status: Accepted

Solution code:
import sys
A=range(14)
m=[[0 for i in A]for j in A]
def f(y,x):
    if m[y][x]==1:
        m[y][x]=0
        f(y,x+1)
        f(y,x-1)
        f(y+1,x)
        f(y-1,x)
    return 1
def solve():
    c=0
    for i in A:
        while m[i].count(1)>0:c+=f(i,m[i].index(1))
    print c
    return 0
y=0
for s in sys.stdin:
    if y<12:
        y+=1
        m[y]=[0]+map(int,list(s[:-1]))+[0]
    else:y=solve()
solve()

Submission status: Accepted

Solution code:
l = 12
def island(f,x,y,m):
    rf = f[:][:]
    if rf[y][x] == 1:
        rf[y][x] = m
        for i in [-1,1]:
            if 0 <= x+i <= l-1:
                rf = island(rf,x+i,y,m)
            if 0 <= y+i <= l-1:
                rf = island(rf,x,y+i,m)
    return rf

while True:
    f = []
    m = 2
    for i in range(l):
        f.append(map(int, list(raw_input())))
    for y in range(l):
        for x in range(l):
            f = island(f,x,y,m)
            m += 1

    ans = []
    for i in range(l):
        for j in range(l):
            ans.append(f[i][j])
    ans = list(set(ans))
    if ans.count(0) == 1:
        print len(ans) - 1
    else:
        print len(ans)
    try   : raw_input()
    except: break

Submission status: Accepted

Solution code:
def island(f,x,y,m):
    rf = f[:][:]
    rf[y][x] = m
    for i in [-1,1]:
        if 0 <= x+i <= 11 and rf[y][x+i] == 1:
            rf = island(rf,x+i,y,m)
        if 0 <= y+i <= 11 and rf[y+i][x] == 1:
            rf = island(rf,x,y+i,m)
    return rf

while True:
    f = []; m = 2
    for i in range(12):
        f.append(map(int, list(raw_input())))
    for y in range(12):
        for x in range(12):
            if f[y][x] == 1:
                f = island(f,x,y,m)
                m += 1
    ans = []
    for i in range(12):
        for j in range(12):
            ans.append(f[i][j])
    ans = list(set(ans))
    print len(ans) - 1*ans.count(0)
    try:raw_input()
    except: break

Submission status: Accepted

Solution code:
dx, dy = [-1, 0, 1, 0], [0, -1, 0, 1]
while True:
    ans = 0
    try:
        m = ['0'*14] + ['0'+raw_input()+'0' for i in xrange(12)] + ['0'*14]
        f = [[False for i in xrange(14)] for j in xrange(14)]
        for y in xrange(1, 13):
            for x in xrange(1, 13):
                if m[y][x]=='1' and f[y][x]==False:
                    q = [[x, y]]
                    f[y][x] = True
                    while len(q)>0:
                        px, py = q.pop(0)
                        for k in xrange(4):
                            nx = px + dx[k]; ny = py + dy[k];
                            if m[ny][nx]=='1' and f[ny][nx]==False:
                                f[ny][nx] = True
                                q.append([nx, ny])
                    ans += 1
        print ans
        raw_input()
    except EOFError:
        break

Submission status: Accepted

Solution code:
W,H = 12,12
dxy = [[1,0],[0,1],[-1,0],[0,-1]]
def solve(field,w,h):
	if 0 <= w < W and 0 <= h < H and field[h][w] == "1":
		field[h][w] = "0"
		for dx,dy in dxy:
			solve(field,w+dx,h+dy)

while 1:
	try:
		field = [list(raw_input()) for i in range(H)]
		ans = 0
		for h in range(H):
			for w in range(W):
				if field[h][w] == "1":
					solve(field,w,h)
					ans += 1
		print ans
		raw_input()
	except:
		break

Submission status: Accepted

Solution code:
def get_map():
  map = []
  while True:
    try:
      tmp = list(raw_input())
      if len(tmp) != 12: break
      map.append(tmp)
    except:
      break
  return map

def remove_island(x, y, map):
  map[x][y] = 0
  move = [[1, 0], [0, 1], [-1, 0], [0, -1]]
  for i, j in move:
    if 0 <= x + i <= 11 and 0 <= y + j <= 11 and map[x + i][y + j] == '1':
      map = remove_island(x + i, y + j, map)
  return map[:]

while True:
  map = get_map()
  if len(map) != 12: break

  count = 0
  for x in range(12):
    for y in range(12):
      if map[x][y] == '1':
        count += 1
        map = remove_island(x, y, map)
  print count

Submission status: Accepted

Solution code:
H, W = 12, 12
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]


def dfs(i, j):
    field[i][j] = 0
    for k in xrange(4):
        ny = i + dy[k]
        nx = j + dx[k]
        if nx < 0 or ny < 0 or nx >= W or ny >= H:
            continue
        if field[ny][nx] == 1:
            dfs(ny, nx)

while 1:
    field = []
    for i in xrange(H):
        field.append(map(int, raw_input()))

    ret = 0
    for i in xrange(H):
        for j in xrange(W):
            if field[i][j] == 1:
                dfs(i, j)
                ret += 1
    print ret
    try:
        raw_input()
    except EOFError:
        break

Submission status: Accepted

Solution code:
def count_island(ban):
    def remove(x, y):
        if 0 <= y < 12 and 0 <= x < 12 and ban[y][x] == 1:
            ban[y][x] = 0
            for dx, dy in [(-1,0), (1,0), (0,-1), (0,1)]:
                remove(x + dx, y + dy)

    count = 0
    for y in range(12):
        for x in range(12):
            if ban[y][x] == 1:
                count += 1
                remove(x, y)

    print(count)

ban = []
while True:
    try:
       s = input()
    except EOFError:
        break

    if s:
        ban.append(list(map(int, s)))
    else:
        count_island(ban)
        ban = []

if ban: count_island(ban)

Submission status: Accepted

Solution code:

def dfs(L,i,j):
    if L[i][j] > 0:
        L[i][j] = 0
    else:
        return

    for k in [-1, 1]:
        ni = i + k
        if ni not in range(12):
            continue
        if L[ni][j] > 0:
            dfs(L, ni, j)

    for k in [-1, 1]:
        nj = j + k
        if nj not in range(12):
            continue
        if L[i][nj] > 0:
            dfs(L, i, nj)
    return

while True:
    L = []
    try:
        for _ in range(12):
            L.append( [int(x) for x in input()] )
    except:
        break

    c = 0
    for i in range(12):
        for j in range(12):
            if L[i][j]  > 0:
                c += 1
                dfs(L, i, j)
    print(c)
    try:
        input()
    except:
        break

Submission status: Accepted

Solution code:
dx=[1,0,-1,0]
dy=[0,1,0,-1]
def dfs(i,j,m):
    m[i][j] = 0
    for k in range(4):
        x = i + dx[k]
        y = j + dy[k]
        if (x < 12 and x >= 0 and y < 12 and y >= 0 and m[x][y] == 1):
            dfs(x,y,m)
    return
while True:
    try:
        m = []
        sum = 0
        for i in range(12):
            m.append(list(map(int, input())))
        for i in range(12):
            for j in range(12):
                if (m[i][j] == 1):
                    dfs(i,j,m)
                    sum += 1
        print(sum)
        input()
    except:
        break

Submission status: Accepted

Solution code:

def dfs(A,x,y):

	X = [1,0,-1,0]
	Y = [0,1,0,-1]

	A[x][y] = 0

	for i in range(4):
		xx = x + X[i]
		yy = y + Y[i]

		if 0 <= xx < 12 and 0 <= yy < 12:
			if A[xx][yy] == "1":
				dfs(A,xx,yy)

if __name__ == '__main__':

	A = []

	while True:
		try:
			for _ in range(12):
				ll = list(input())
				A.append(ll)

			iland = 0
			for x in range(12):
				for y in range(12):
					if A[x][y] == "1":
						iland += 1
						dfs(A,x,y)
			print(iland)

			s = input()
			A=[]

		except EOFError:
			break

Submission status: Accepted

Solution code:
import sys
dataset = []
lst = []
for line in sys.stdin:
	lst.append(line)
minilist = []
for i in range(0, len(lst)):
	if lst[i] == '\n':
		dataset.append(minilist)
		minilist = []
	else: minilist.append(lst[i])
dataset.append(minilist)

for i in range(0, len(dataset)):
	memo = []
	for j in range(0, 12):
		lst = []
		for k in range(0, 12):
			lst.append(True)
		memo.append(lst)
	cnt = 0
	for j in range(0, 12):
		for k in range(0, 12):
			if dataset[i][j][k] == '1' and memo[j][k]:
				queue = [[j, k]]
				while queue != []:
					now = queue.pop(0)
					if memo[now[0]][now[1]] and dataset[i][now[0]][now[1]]=='1':
						memo[now[0]][now[1]] = False
						for p in range(0, 2):
							nexth = now[0]-1+2*p
							nextw = now[1]-1+2*p
							if 0<=nexth and nexth<12:
								queue.append([nexth, now[1]])
							if 0<=nextw and nextw<12:
								queue.append([now[0], nextw])
				cnt += 1
	print cnt

Submission status: Accepted

Solution code:
state = 1
while(state):
    a = [[0 for i in range(12)] for j in range(12)]
    for i in range(12):
        a[i] = [int(i) for i in list(input())]
    try:
        _ = input()
    except EOFError:
        state = 0

    def erase(y,x):
        if a[y][x] != 0:
            a[y][x] = 0
            if x-1 > -1:
                erase(y,x-1)
            if x+1 < 12:
                erase(y,x+1)
            if y-1 > -1:
                erase(y-1,x)
            if y+1 < 12:
                erase(y+1,x)

    count = 0
    for i in range(12):
        for j in range(12):
            if a[i][j] != 0:
                count += 1
                erase(i,j)
    print(count)

Submission status: Accepted

Solution code:


def dfs(land,y,x):
    land[y][x] = '0'
    if y > 0:
        if land[y-1][x] == '1':
            dfs(land,y-1,x)
    if y != 11:
        if (land[y+1][x] == '1'):
            dfs(land,y+1,x)
    if (x > 0):
        if  (land[y][x-1] == '1'):
            dfs(land,y,x-1)
    if (x != 11) :
        if  (land[y][x+1] == '1'):
            dfs(land,y,x+1)

island = []

while True:
    try:
        ss = input()
        if ss :
            island.append(list(ss))
    except:
        break
        
num = int(len(island) / 12)

for i in range(num):
    count = 0
    for j in range(12):
        for k in range(12):
            if (island[i*12+j][k] == '1'): 
                dfs(island[i*12:(i+1)*12],j,k)
                count += 1

    print(count)

Submission status: Runtime Error

Solution code:
def isearch(field,dir1,dir2,I,J,Q):
    field[I][J]=0
    for d in dir1:
        if field[min(11,I+d[0])][min(11,J+d[1])]:
            Q.append([I+d[0],J+d[1]])
    for d in dir2:
        if field[max(0,I+d[0])][max(0,J+d[1])]:
            Q.append([I+d[0],J+d[1]])
    if len(Q)!=0:
        [i,j]=Q.pop(0)
        isearch(field,dir1,dir2,i,j,Q)
    

while(1):
    field=[]
    for i in range(12):
        field.append([int(x) for x in raw_input()])
    Q=[]
    N=0
    dir1=[[0,1],[1,0]]
    dir2=[[0,-1],[-1,0]]
    for i in range(12):
        for j in range(12):
            if field[i][j]:
                N=N+1
                isearch(field,dir1,dir2,i,j,Q)
    print N
    try:
        raw_input()
    except:
        break

Submission status: Runtime Error

Solution code:
def isearch(field,dir1,dir2,I,J,Q):
    field[I][J]=0
    for d in dir1:
        if field[min(11,I+d[0])][min(11,J+d[1])]:
            Q.append([I+d[0],J+d[1]])
    for d in dir2:
        if field[max(0,I+d[0])][max(0,J+d[1])]:
            Q.append([I+d[0],J+d[1]])
    if len(Q)!=0:
        [i,j]=Q.pop(0)
        isearch(field,dir1,dir2,i,j,Q)
    

while(1):
    field=[]
    for i in range(12):
        field.append([int(x) for x in raw_input()])
    Q=[]
    N=0
    dir1=[[0,1],[1,0]]
    dir2=[[0,-1],[-1,0]]
    for i in range(12):
        for j in range(12):
            if field[i][j]:
                N=N+1
                isearch(field,dir1,dir2,i,j,Q)
    print N
    try:
        skip=raw_input()
    except:
        break

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000000)


M, N = map(int, raw_input().split())
field = [[0] * (M + 2)]
for i in range(N):
    row = [0]
    row.extend(map(int, raw_input().split()))
    row.append(0)
    field.append(row)
field.append([0] * (M + 2))


def search(row, column):
    global field
    if field[row][column] == 0:
        return 0
    field[row][column] = 0
    search(row - 1, column)
    search(row + 1, column)
    search(row, column + 1)
    search(row, column - 1)
    return 1

ans = 0
for row in range(N):
    for column in range(M):
        ans += search(row + 1, column + 1)
print ans

Submission status: Runtime Error

Solution code:
import sys

def quartet(h, w):
    global flag
    if 0 <= h <= 11 and 0 <= w <= 11:
        if mapp[h][w] == '1':
            mapp[h][w] = '$'
            flag = True
            bigbang(h, w)
        elif mapp[h][w] == '0' or mapp[h][w] == '$':
            return
    else:
        return

def bigbang(h, w):
    quartet(h, w+1)
    quartet(h-1,w)
    quartet(h, w-1)
    quartet(h+1, w)

while True:
    try:
        mapp = []
        for line in sys.stdin:
            lis = []
            for char in line.rstrip():
                lis.append(char)
            else:
                mapp.append(lis)
        
        flag = False
        count = 0
        for h in range(12):
            for w in range(12):
                quartet(h, w)
                if flag == True:
                    count += 1
                flag = False
        else:
            print count
        if raw_input == '':
            continue
    except EOFError:
        break

Submission status: Runtime Error

Solution code:
import sys

def quartet(h, w):
    global flag
    if 0 <= h <= 11 and 0 <= w <= 11:
        if mapp[h][w] == '1':
            mapp[h][w] = '$'
            flag = True
            bigbang(h, w)
        elif mapp[h][w] == '0' or mapp[h][w] == '$':
            return
    else:
        return

def bigbang(h, w):
    quartet(h, w+1)
    quartet(h-1,w)
    quartet(h, w-1)
    quartet(h+1, w)

while True:
    try:
        mapp = []
        for i in range(12):
            line = raw_input()
            lis = []
            for char in line:
                lis.append(char)
            else:
                mapp.append(lis)
                
        flag = False
        count = 0
        for h in range(12):
            for w in range(12):
                quartet(h, w)
                if flag == True:
                    count += 1
                flag = False
        else:
            print count
        if raw_input == '':
            continue
    except EOFError:
        break

Submission status: Runtime Error

Solution code:
class Depth_First_Search_stack:
    def __init__(self, map):
        self.map = map
        self.num_island = 0
        self.stack = []
        self.start_point = None

    def get_start_point(self):
        get_start = False
        for i in range(12):
            for j in range(12):
                if self.map[i][j] == '1':
                    self.start_point = [i, j]
                    get_start = True
        return get_start

    def check(self, new_r, new_c):
        return 0<=new_r<12 and 0<=new_c<12 and self.map[new_r][new_c] == '1'

    def search(self, r, c):
        while self.stack:
            r, c = self.stack.pop()
            self.map[r][c] = '0'
            for (i, j) in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
                if self.check(r+i, c+j):
                    self.stack.append([r+i, c+j])

    def run(self):
        while self.get_start_point():
            r, c = self.start_point
            self.stack.append([r, c])
            self.search(r, c)
            self.num_island += 1

if __name__ == '__main__':
    while 1:
        map = []
        for i in range(12):
            row = [x for x in input()]
            map.append(row)

        if len(map[0]) != 12:
            break

        DFS = Depth_First_Search_stack(map)
        DFS.run()
        print(DFS.num_island)

        empty = input()

Submission status: Runtime Error

Solution code:
class Depth_First_Search_stack:
    def __init__(self, map):
        self.map = map
        self.num_island = 0
        self.stack = []
        self.start_point = None

    def get_start_point(self):
        get_start = False
        for i in range(12):
            for j in range(12):
                if self.map[i][j] == '1':
                    self.start_point = [i, j]
                    get_start = True
        return get_start

    def check(self, new_r, new_c):
        return 0<=new_r<12 and 0<=new_c<12 and self.map[new_r][new_c] == '1'

    def search(self, r, c):
        while self.stack:
            r, c = self.stack.pop()
            self.map[r][c] = '0'
            for (i, j) in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
                if self.check(r+i, c+j):
                    self.stack.append([r+i, c+j])

    def run(self):
        while self.get_start_point():
            r, c = self.start_point
            self.stack.append([r, c])
            self.search(r, c)
            self.num_island += 1

if __name__ == '__main__':
    while 1:
        stop_flag = False
        map = []
        for i in range(12):
            row = [x for x in input()]
            if i==0:
                if len(row) != 12:
                    stop_flag = True
                    break
            map.append(row)
        if stop_flag:
            break

        DFS = Depth_First_Search_stack(map)
        DFS.run()
        print(DFS.num_island)

        empty = input()

Submission status: Runtime Error

Solution code:
class Depth_First_Search_stack:
    def __init__(self, map):
        self.map = map
        self.num_island = 0
        self.stack = []
        self.start_point = None

    def get_start_point(self):
        get_start = False
        for i in range(12):
            for j in range(12):
                if self.map[i][j] == '1':
                    self.start_point = [i, j]
                    get_start = True
        return get_start

    def check(self, new_r, new_c):
        return 0<=new_r<12 and 0<=new_c<12 and self.map[new_r][new_c] == '1'

    def search(self, r, c):
        while self.stack:
            r, c = self.stack.pop()
            self.map[r][c] = '0'
            for (i, j) in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
                if self.check(r+i, c+j):
                    self.stack.append([r+i, c+j])

    def run(self):
        while self.get_start_point():
            r, c = self.start_point
            self.stack.append([r, c])
            self.search(r, c)
            self.num_island += 1

if __name__ == '__main__':
    for t in range(20):
        stop_flag = False
        map = []
        for i in range(12):
            row = [x for x in input()]
            if i==0:
                if len(row) != 12:
                    stop_flag = True
                    break
            map.append(row)
        if stop_flag:
            break

        DFS = Depth_First_Search_stack(map)
        DFS.run()
        print(DFS.num_island)

        empty = input()

Submission status: Runtime Error

Solution code:
class Depth_First_Search_stack:
    def __init__(self, map):
        self.map = map
        self.num_island = 0
        self.stack = []
        self.start_point = None

    def get_start_point(self):
        get_start = False
        for i in range(12):
            for j in range(12):
                if self.map[i][j] == '1':
                    self.start_point = [i, j]
                    get_start = True
        return get_start

    def check(self, new_r, new_c):
        return 0<=new_r<12 and 0<=new_c<12 and self.map[new_r][new_c] == '1'

    def search(self, r, c):
        while self.stack:
            r, c = self.stack.pop()
            self.map[r][c] = '0'
            for (i, j) in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
                if self.check(r+i, c+j):
                    self.stack.append([r+i, c+j])

    def run(self):
        while self.get_start_point():
            r, c = self.start_point
            self.stack.append([r, c])
            self.search(r, c)
            self.num_island += 1

if __name__ == '__main__':
    for t in range(20):
        stop_flag = False
        map = []
        for i in range(12):
            row = [x for x in input()]
            if i==0:
                if len(row) != 12:
                    stop_flag = True
                    break
            map.append(row)

        if stop_flag:
            break

        DFS = Depth_First_Search_stack(map)
        DFS.run()
        print(DFS.num_island)
        empty = input()

Submission status: Runtime Error

Solution code:
class Depth_First_Search_stack:
    def __init__(self, map):
        self.map = map
        self.num_island = 0
        self.stack = []
        self.start_point = None

    def get_start_point(self):
        get_start = False
        for i in range(12):
            for j in range(12):
                if self.map[i][j] == '1':
                    self.start_point = [i, j]
                    get_start = True
        return get_start

    def check(self, new_r, new_c):
        if not(0<=new_r<12 and 0<=new_c<12):
            return False
        else:
            return self.map[new_r][new_c] == '1'

    def search(self, r, c):
        self.stack.append(self.start_point)
        while self.stack:
            r, c = self.stack.pop()
            self.map[r][c] = '0'
            for (i, j) in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
                if self.check(r+i, c+j):
                    self.stack.append([r+i, c+j])

    def run(self):
        while self.get_start_point():
            r, c = self.start_point
            self.search(r, c)
            self.num_island += 1

if __name__ == '__main__':
    for t in range(20):
        stop_flag = False
        map = []
        for i in range(12):
            row = [x for x in input()]
            if i==0:
                if len(row) != 12:
                    stop_flag = True
                    break
            map.append(row)

        if stop_flag:
            break

        DFS = Depth_First_Search_stack(map)
        DFS.run()
        print(DFS.num_island)
        empty = input()

Submission status: Runtime Error

Solution code:
mx = [0, 1, 0, -1]
my = [-1, 0, 1, 0]

while True:
    try:
        board = [[int(i) for i in input()] for j in range(12)]
    except:
        break
    cnt = 0

    for y in range(12):
        for x in range(12):
            if board[y][x] == 1:
                cnt += 1
                q = [(y, x)]
                while len(q) > 0:
                    new_q = []
                    for yy, xx in q:
                        board[yy][xx] = 0
                        for i in range(4):
                            nx, ny = xx + mx[i], yy + my[i]
                            if 0 <= nx < 12 and 0 <= ny < 12 and board[ny][nx]:
                                new_q.append((ny, nx))
                    q = new_q

    print(cnt)

Submission status: Runtime Error

Solution code:
mx = [0, 1, 0, -1]
my = [-1, 0, 1, 0]

while True:
    try:
        board = [[int(i) for i in input()] for j in range(12)]
    except EOFError:
        break
    cnt = 0

    for y in range(12):
        for x in range(12):
            if board[y][x] == 1:
                cnt += 1
                q = [(y, x)]
                while len(q) > 0:
                    new_q = []
                    for yy, xx in q:
                        board[yy][xx] = 0
                        for i in range(4):
                            nx, ny = xx + mx[i], yy + my[i]
                            if 0 <= nx < 12 and 0 <= ny < 12 and board[ny][nx]:
                                new_q.append((ny, nx))
                    q = new_q

    print(cnt)

Submission status: Runtime Error

Solution code:
mx = [0, 1, 0, -1]
my = [-1, 0, 1, 0]

while True:
    try:
        board = [[int(i) for i in input()] for j in range(12)]
        cnt = 0

        for y in range(12):
            for x in range(12):
                if board[y][x] == 1:
                    cnt += 1
                    q = [(y, x)]
                    while len(q) > 0:
                        new_q = []
                        for yy, xx in q:
                            board[yy][xx] = 0
                            for i in range(4):
                                nx, ny = xx + mx[i], yy + my[i]
                                if 0 <= nx < 12 and 0 <= ny < 12 and board[ny][nx]:
                                    new_q.append((ny, nx))
                        q = new_q

        print(cnt)
    except EOFError:
        break

Submission status: Runtime Error

Solution code:
mx = [0, 1, 0, -1]
my = [-1, 0, 1, 0]

while True:
    try:
        board = [[int(i) for i in input()] for j in range(12)]
    except EOFError:
        break
    cnt = 0

    for y in range(12):
        for x in range(12):
            if board[y][x] == 1:
                cnt += 1
                q = [(y, x)]
                while len(q) > 0:
                    new_q = []
                    for yy, xx in q:
                        board[yy][xx] = 0
                        for i in range(4):
                            nx, ny = xx + mx[i], yy + my[i]
                            if 0 <= nx < 12 and 0 <= ny < 12 and board[ny][nx]:
                                new_q.append((ny, nx))
                    q = new_q
    
    print(cnt)
    input()

Submission status: Runtime Error

Solution code:
mx = [0, 1, 0, -1]
my = [-1, 0, 1, 0]

while True:
    try:
        board = [[int(i) for i in input()] for j in range(12)]
    except EOFError:
        break
    cnt = 0

    for y in range(12):
        for x in range(12):
            if board[y][x] == 1:
                cnt += 1
                q = [(y, x)]
                while len(q) > 0:
                    new_q = []
                    for yy, xx in q:
                        board[yy][xx] = 0
                        for i in range(4):
                            nx, ny = xx + mx[i], yy + my[i]
                            if 0 <= nx < 12 and 0 <= ny < 12 and board[ny][nx] and (ny, nx) not in new_q:
                                new_q.append((ny, nx))
                    q = new_q
    print(cnt)
    input()

Submission status: Runtime Error

Solution code:
def dfs(i, j, m):
    '''
    m[i][j]??¨????????£??????????????°????????????????????????
    '''
    m[i][j] = 0
    for di, dj in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        ni = i + di
        nj = j + dj
        if ni in range(12) and nj in range(12) and m[ni][nj] == 1:
            dfs(ni, nj, m)                

while True:
    m = []
    for i in range(12):
        m.append(list(map(int, list(input()))))
    
    count = 0
    for i in range(12):
        for j in range(12):
            if m[i][j] == 1:
                count += 1
                dfs(i, j, m)
    
    print(count)

Submission status: Runtime Error

Solution code:
def dfs(i, j, m):
    '''
    m[i][j]??¨????????£??????????????°????????????????????????
    '''
    m[i][j] = 0
    for di, dj in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        ni = i + di
        nj = j + dj
        if ni in range(12) and nj in range(12) and m[ni][nj] == 1:
            dfs(ni, nj, m)                

while True:
    m = []
    for i in range(12):
        m.append(list(map(int, list(input()))))
    
    count = 0
    for i in range(12):
        for j in range(12):
            if m[i][j] == 1:
                count += 1
                dfs(i, j, m)
    
    print(count)
    input()