Problem p02803 (python)

Fixed submission:
from copy import deepcopy
from collections import deque
import sys

M = []
H = 0
W = 0
qx = deque()
qy = deque()
qc = deque()

def main():
    global M,H,W
    H,W = map(int, input().split())  
    for i in range(H):
        M.append(list(input()))

    startX = 0
    startY = 0
    mx = 0
    for i in range(H):
        for j in range(W):
            if M[i][j]=='.':
                rv =  bfs(i,j)
                if rv > mx:
                    mx = rv


    print(mx)

def bfs(y,x):
    global M,H,W
    Mtemp = deepcopy(M)
    Mtemp[y][x] = '#'
    qx.append(x)
    qy.append(y)
    qc.append(0)
    cnt = 0

    while len(qx) !=0:
        x =  qx.popleft()
        y =  qy.popleft()
        cnt = qc.popleft()

        if x-1>=0:
            if Mtemp[y][x-1] == '.':
                Mtemp[y][x-1] = '#'
                qx.append(x-1)
                qy.append(y)
                qc.append(cnt+1)
        if x+1<W:
            if Mtemp[y][x+1] == '.':
                Mtemp[y][x+1] = '#'
                qx.append(x+1)
                qy.append(y)
                qc.append(cnt+1)
        if y-1>=0:
            if Mtemp[y-1][x] == '.':
                Mtemp[y-1][x] = '#'
                qx.append(x)
                qy.append(y-1)
                qc.append(cnt+1)
        if y+1<H:
            if Mtemp[y+1][x] == '.':
                Mtemp[y+1][x] = '#'
                qx.append(x)
                qy.append(y+1)
                qc.append(cnt+1)

    return cnt




if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from copy import deepcopy
from collections import deque
import sys

M = []
H = 0
W = 0
qx = deque()
qy = deque()
qc = deque()

def main():
    global M,H,W
    H,W = map(int, input().split())  
    for i in range(H):
        M.append(list(input()))

    startX = 0
    startY = 0
    for i in range(H):
        startX = M[i].index('.')
        if startX >= 0:
            startY = i
            break

    Mtemp = deepcopy(M)

    Mtemp[startY][startX] = '#'
    qx.append(startX)
    qy.append(startY)
    qc.append(0)
    cnt = 0
    x = 0 
    y = 0

    while len(qx) !=0:
        x =  qx.popleft()
        y =  qy.popleft()
        cnt = qc.popleft()

        if x-1>=0:
            if Mtemp[y][x-1] == '.':
                Mtemp[y][x-1] = '#'
                qx.append(x-1)
                qy.append(y)
                qc.append(cnt+1)
        if x+1<W:
            if Mtemp[y][x+1] == '.':
                Mtemp[y][x+1] = '#'
                qx.append(x+1)
                qy.append(y)
                qc.append(cnt+1)
        if y-1>=0:
            if Mtemp[y-1][x] == '.':
                Mtemp[y-1][x] = '#'
                qx.append(x)
                qy.append(y-1)
                qc.append(cnt+1)
        if y+1<H:
            if Mtemp[y+1][x] == '.':
                Mtemp[y+1][x] = '#'
                qx.append(x)
                qy.append(y+1)
                qc.append(cnt+1)

    Mtemp = deepcopy(M)

    Mtemp[y][x] = '#'
    qx.append(x)
    qy.append(y)
    qc.append(0)
    cnt = 0

    while len(qx) !=0:
        x =  qx.popleft()
        y =  qy.popleft()
        cnt = qc.popleft()

        if x-1>=0:
            if Mtemp[y][x-1] == '.':
                Mtemp[y][x-1] = '#'
                qx.append(x-1)
                qy.append(y)
                qc.append(cnt+1)
        if x+1<W:
            if Mtemp[y][x+1] == '.':
                Mtemp[y][x+1] = '#'
                qx.append(x+1)
                qy.append(y)
                qc.append(cnt+1)
        if y-1>=0:
            if Mtemp[y-1][x] == '.':
                Mtemp[y-1][x] = '#'
                qx.append(x)
                qy.append(y-1)
                qc.append(cnt+1)
        if y+1<H:
            if Mtemp[y+1][x] == '.':
                Mtemp[y+1][x] = '#'
                qx.append(x)
                qy.append(y+1)
                qc.append(cnt+1)


    print(cnt)




if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from copy import deepcopy
from collections import deque
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

M = []
vx = [0,1,0,-1]
vy = [1,0,-1,0]
H = 0
W = 0
qx = deque()
qy = deque()
qc = deque()

def main():
    global M,H,W
    H,W = map(int, input().split())  
    for i in range(H):
        M.append(list(input()))

    startX = 0
    startY = 0
    for i in range(H):
        startX = M[i].index('.')
        if startX >= 0:
            startY = i
            break

    Mtemp = deepcopy(M)

    Mtemp[startY][startX] = '#'
    qx.append(startX)
    qy.append(startY)
    qc.append(0)
    cnt = 0
    x = 0 
    y = 0

    while len(qx) !=0:
        x =  qx.popleft()
        y =  qy.popleft()
        cnt = qc.popleft()

        if x-1>=0:
            if Mtemp[y][x-1] == '.':
                Mtemp[y][x-1] = '#'
                qx.append(x-1)
                qy.append(y)
                qc.append(cnt+1)
        if x+1<W:
            if Mtemp[y][x+1] == '.':
                Mtemp[y][x+1] = '#'
                qx.append(x+1)
                qy.append(y)
                qc.append(cnt+1)
        if y-1>=0:
            if Mtemp[y-1][x] == '.':
                Mtemp[y-1][x] = '#'
                qx.append(x)
                qy.append(y-1)
                qc.append(cnt+1)
        if y+1<H:
            if Mtemp[y+1][x] == '.':
                Mtemp[y+1][x] = '#'
                qx.append(x)
                qy.append(y+1)
                qc.append(cnt+1)

    Mtemp = deepcopy(M)

    Mtemp[y][x] = '#'
    qx.append(x)
    qy.append(y)
    qc.append(0)

    while len(qx) !=0:
        x =  qx.popleft()
        y =  qy.popleft()
        cnt = qc.popleft()

        if x-1>=0:
            if Mtemp[y][x-1] == '.':
                Mtemp[y][x-1] = '#'
                qx.append(x-1)
                qy.append(y)
                qc.append(cnt+1)
        if x+1<W:
            if Mtemp[y][x+1] == '.':
                Mtemp[y][x+1] = '#'
                qx.append(x+1)
                qy.append(y)
                qc.append(cnt+1)
        if y-1>=0:
            if Mtemp[y-1][x] == '.':
                Mtemp[y-1][x] = '#'
                qx.append(x)
                qy.append(y-1)
                qc.append(cnt+1)
        if y+1<H:
            if Mtemp[y+1][x] == '.':
                Mtemp[y+1][x] = '#'
                qx.append(x)
                qy.append(y+1)
                qc.append(cnt+1)


    print(cnt)




if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from copy import deepcopy
from collections import deque
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

M = []
vx = [0,1,0,-1]
vy = [1,0,-1,0]
H = 0
W = 0
qx = deque()
qy = deque()
qc = deque()

def main():
    global M,H,W
    H,W = map(int, input().split())  
    for i in range(H):
        M.append(list(input()))

    startX = 0
    startY = 0
    for i in range(H):
        startX = M[i].index('.')
        if startX >= 0:
            startY = i
            break

    Mtemp = deepcopy(M)

    Mtemp[startY][startX] = '#'
    qx.append(startX)
    qy.append(startY)
    qc.append(0)
    cnt = 0
    x = 0 
    y = 0

    while len(qx) !=0:
        x =  qx.popleft()
        y =  qy.popleft()
        cnt = qc.popleft()

        if x-1>=0:
            if Mtemp[x-1][y] == '.':
                Mtemp[x-1][y] = '#'
                qx.append(x-1)
                qy.append(y)
                qc.append(cnt+1)
        if x+1<W:
            if Mtemp[x+1][y] == '.':
                Mtemp[x+1][y] = '#'
                qx.append(x+1)
                qy.append(y)
                qc.append(cnt+1)
        if y-1>=0:
            if Mtemp[x][y-1] == '.':
                Mtemp[x][y-1] = '#'
                qx.append(x)
                qy.append(y-1)
                qc.append(cnt+1)
        if y+1<H:
            if Mtemp[x][y+1] == '.':
                Mtemp[x][y+1] = '#'
                qx.append(x)
                qy.append(y+1)
                qc.append(cnt+1)

    Mtemp = deepcopy(M)

    Mtemp[x][y] = '#'
    qx.append(x)
    qy.append(y)
    qc.append(0)

    while len(qx) !=0:
        x =  qx.popleft()
        y =  qy.popleft()
        cnt = qc.popleft()

        if x-1>=0:
            if Mtemp[x-1][y] == '.':
                Mtemp[x-1][y] = '#'
                qx.append(x-1)
                qy.append(y)
                qc.append(cnt+1)
        if x+1<W:
            if Mtemp[x+1][y] == '.':
                Mtemp[x+1][y] = '#'
                qx.append(x+1)
                qy.append(y)
                qc.append(cnt+1)
        if y-1>=0:
            if Mtemp[x][y-1] == '.':
                Mtemp[x][y-1] = '#'
                qx.append(x)
                qy.append(y-1)
                qc.append(cnt+1)
        if y+1<H:
            if Mtemp[x][y+1] == '.':
                Mtemp[x][y+1] = '#'
                qx.append(x)
                qy.append(y+1)
                qc.append(cnt+1)



    print(cnt)




if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from copy import deepcopy
from collections import deque
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

M = []
vx = [0,1,0,-1]
vy = [1,0,-1,0]
H = 0
W = 0
qx = deque()
qy = deque()
qc = deque()

def main():
    global M,H,W
    H,W = map(int, input().split())  
    for i in range(H):
        M.append(list(input()))

    startX = 0
    startY = 0
    for i in range(H):
        startX = M[i].index('.')
        if startX >= 0:
            startY = i
            break

    Mtemp = deepcopy(M)

    Mtemp[startY][startX] = '#'
    qx.append(startX)
    qy.append(startY)
    qc.append(0)
    cnt = 0
    x = 0 
    y = 0

    while len(qx) !=0:
        x =  qx.popleft()
        y =  qy.popleft()
        cnt = qc.popleft()

        if x-1>=0:
            if Mtemp[x-1][y] == '.':
                Mtemp[x-1][y] = '#'
                qx.append(x-1)
                qy.append(y)
                qc.append(cnt+1)
        if x+1<H:
            if Mtemp[x+1][y] == '.':
                Mtemp[x+1][y] = '#'
                qx.append(x+1)
                qy.append(y)
                qc.append(cnt+1)
        if y-1>=0:
            if Mtemp[x][y-1] == '.':
                Mtemp[x][y-1] = '#'
                qx.append(x)
                qy.append(y-1)
                qc.append(cnt+1)
        if y+1<W:
            if Mtemp[x][y+1] == '.':
                Mtemp[x][y+1] = '#'
                qx.append(x)
                qy.append(y+1)
                qc.append(cnt+1)

    Mtemp = deepcopy(M)

    Mtemp[x][y] = '#'
    qx.append(x)
    qy.append(y)
    qc.append(0)

    while len(qx) !=0:
        x =  qx.popleft()
        y =  qy.popleft()
        cnt = qc.popleft()

        if x-1>=0:
            if Mtemp[x-1][y] == '.':
                Mtemp[x-1][y] = '#'
                qx.append(x-1)
                qy.append(y)
                qc.append(cnt+1)
        if x+1<H:
            if Mtemp[x+1][y] == '.':
                Mtemp[x+1][y] = '#'
                qx.append(x+1)
                qy.append(y)
                qc.append(cnt+1)
        if y-1>=0:
            if Mtemp[x][y-1] == '.':
                Mtemp[x][y-1] = '#'
                qx.append(x)
                qy.append(y-1)
                qc.append(cnt+1)
        if y+1<W:
            if Mtemp[x][y+1] == '.':
                Mtemp[x][y+1] = '#'
                qx.append(x)
                qy.append(y+1)
                qc.append(cnt+1)



    print(cnt)




if __name__ == '__main__':
    main()