Problem p03053 (python)

Fixed submission:
#!/usr/bin/env python3
#AGC33 A

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
maze = [list(input()) for _ in range(h)]
check = [[True]*w for _ in range(h)]
dx,dy = [0,1,0,-1],[1,0,-1,0]
que = deque()
for i in range(h):
    for j in range(w):
        if maze[i][j] == '#':
            check[i][j] = False
            que.append((j,i))
res = 0
while que:
    que2 = deque(que)
    que = deque()
    flg = False
    while que2:
        x,y = que2.popleft()
        for i in range(4):
            nx,ny = x + dx[i], y + dy[i]
            if 0 <= nx < w and 0 <= ny < h:
                if check[ny][nx]:
                    check[ny][nx] = False
                    que.append((nx,ny))
                    flg = True
    if flg:
        res += 1
print(res)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
#A
import re

a,b = map(int,input().split())

c = [list(map(str,input().split()))for i in range(a)]
d = [[0 for j in range(b)]  for i in range(a)]

for i in range(a):#初期のリスト設定
    for j in range(b):
        if c[i][0][j] == '#':
            d[i][j] = 1

def change(save,d):
    for i in save:
        d[i[0]][i[1]] = 1

count = 0
for k in range(a*b):
    save = []
    for i in range(a):
        for j in range(b):
            if d[i][j] == 1:
                if i != 0 and d[i-1][j] == 0:
                        save.append((i-1,j))
                if j != 0 and d[i][j-1] == 0:
                        save.append((i,j-1))
                if i != a-1 and d[i+1][j] == 0:
                        save.append((i+1,j))
                if j != b-1 and d[i][j+1] == 0:
                        save.append((i,j+1))

    change(save,d)
    if len(save) == 0:
        break;
    count += 1
print(count)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
#A
import numpy as np
import math
import re

a,b = map(int,input().split())

c = [list(map(str,input().split()))for i in range(a)]
d = [[0 for j in range(b)]  for i in range(a)]

for i in range(a):#初期のリスト設定
    for j in range(b):
        if c[i][0][j] == '#':
            d[i][j] = 1

def change(save,d):
    for i in save:
        d[i[0]][i[1]] = 1

count = 0
for k in range(a*b):
    count += 1
    save = []
    save2 = []
    for i in range(a):
        for j in range(b):
            if d[i][j] == 1:
                if i != 0:#上があるなら
                    save.append((i-1,j))
                if j != 0:#左があるなら
                    save.append((i,j-1))
                if i != a-1:#下があるなら
                    save.append((i+1,j))
                if j != b-1:#右があるなら
                    save.append((i,j+1))
            else:
                save2.append(1)
    change(save,d)
    if len(save2) == 0:
        break;
print(count-1)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
#A
import numpy as np
import math
import re

a,b = map(int,input().split())

c = [list(map(str,input().split()))for i in range(a)]
d = [[0 for j in range(b)]  for i in range(a)]
e = [[0 for j in range(b)]  for i in range(a)]#コピー用
#print(c,d,e)

for i in range(a):#初期のリスト設定
    for j in range(b):
        if c[i][0][j] == '#':
            d[i][j] = 1
            e[i][j] = 1
#print(c,d,e)

def check(list,a,b):#全て1であるかの判定
    for i in range(a):
        for j in range(b):
            if list[i][j] == 0:
                return False
    return True

def copy(list1,list2,a,b):
    for i in range(a):
        for j in range(b):
            if list2[i][j] == 1:
                list1[i][j] = 1


count = 0
while not check(d,a,b):
    count += 1
    for i in range(a):
        for j in range(b):
            if d[i][j] == 1:
                if i != 0:#上があるなら
                    e[i-1][j] = 1
                if j != 0:#左があるなら
                    e[i][j-1] = 1
                if i != a-1:#下があるなら
                    e[i+1][j] = 1
                if j != a-1:#右があるなら
                    e[i][j+1] = 1
    copy(d,e,a,b)
    check(d,a,b)
print(count)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
#A
import numpy as np
import math
import re

a,b = map(int,input().split())

c = [list(map(str,input().split()))for i in range(a)]
d = [[0 for j in range(a)]  for i in range(a)]
e = [[0 for j in range(a)]  for i in range(a)]#コピー用
#print(c,d,e)

for i in range(a):#初期のリスト設定
    for j in range(a):
        if c[i][0][j] == '#':
            d[i][j] = 1
            e[i][j] = 1
#print(c,d,e)

def check(list):#全て1であるかの判定
    for i in range(len(list)):
        for j in range(len(list)):
            if list[i][j] == 0:
                return False
    return True

def copy(list1,list2):
    for i in range(len(list1)):
        for j in range(len(list1)):
            if list2[i][j] == 1:
                list1[i][j] = 1


count = 0
while not check(d):
    count += 1
    for i in range(a):
        for j in range(a):
            if d[i][j] == 1:
                if i != 0:#上があるなら
                    e[i-1][j] = 1
                if j != 0:#左があるなら
                    e[i][j-1] = 1
                if i != a-1:#下があるなら
                    e[i+1][j] = 1
                if j != a-1:#右があるなら
                    e[i][j+1] = 1
    copy(d,e)
    check(d)
print(count)