Problem p03361 (python)

Fixed submission:
h, w = map(int, input().split())
form = []
flg = 0
for i in range(h):
    form.append(input())
for i in range(h):
    for j in range(w):
        if form[i][j]==".":
            continue
        else:
            if i==0:
                if j==0:
                    if form[i][j+1]== "#" or form[i+1][j]=="#":
                        continue
                    else:
                        flg = 1
                        break
                elif j==w-1:
                    if form[i][j-1]== "#" or form[i+1][j]=="#":
                        continue
                    else:
                        flg = 1
                        break
                else:
                    if form[i][j-1]== "#" or form[i+1][j]=="#" or form[i][j+1]=="#":
                        continue
                    else:
                        flg=1
                        break
                        
            elif i==h-1:    
                if j==0:
                    if form[i][j+1]== "#" or form[i-1][j]=="#":
                        continue
                    else:
                        flg = 1
                        break
                elif j==w-1:
                    if form[i][j-1]== "#" or form[i-1][j]=="#":
                        continue
                    else:
                        flg = 1
                        break
                else:
                    if form[i][j-1]== "#" or form[i-1][j]=="#" or form[i][j+1]=="#":
                        continue
                    else:
                        flg=1
                        break
                
            else:
                if j==0:
                    if form[i][j+1]== "#" or form[i+1][j]=="#" or form[i-1][j]=="#":
                        continue
                    else:
                        flg = 1
                        break
                elif j==w-1:
                    if form[i][j-1]== "#" or form[i+1][j]=="#" or form[i-1][j]=="#":
                        continue
                    else:
                        flg = 1
                        break
                else:
                    if form[i][j-1]== "#"or form[i+1][j]=="#" or form[i][j+1]=="#" or form[i-1][j]=="#":
                        continue
                    else:
                        flg = 1
                        break
    if flg ==1:
        break
    
if flg ==0:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
form = []
flg = 0
for i in range(h):
    form.append(input())
for i in range(h):
    for j in range(w):
        if form[i][j]==".":
            continue
        else:
            if i==0:
                if j==0:
                    if form[i][j+1]== "#" or form[i+1][j]=="#":
                        continue
                    else:
                        flg = 1
                        break
                elif j==w-1:
                    if form[i][j-1]== "#" or form[i+1][j]=="#":
                        continue
                    else:
                        flg = 1
                        break
                else:
                    if form[i][j-1]== "#" or form[i+1][j]=="#" or form[i][j+1]=="#":
                        continue
                    else:
                        flg=1
                        break
                        
            elif i==h-1:    
                if j==0:
                    if form[i][j+1]== "#" or form[i-1][j]=="#":
                        continue
                    else:
                        flg = 1
                        break
                elif j==w-1:
                    if form[i][j-1]== "#" or form[i-1][j]=="#":
                        continue
                    else:
                        flg = 1
                        break
                else:
                    if form[i][j-1]== "#" or form[i-1][j]=="#" or form[i][j+1]=="#":
                        continue
                    else:
                        flg=1
                        break
                
            else:
                if form[i][j-1]== "#"or form[i+1][j]=="#" or form[i][j+1]=="#" or form[i-1][j]=="#":
                    continue
                else:
                    flg = 1
                    break
    if flg ==1:
        break
    
if flg ==0:
    print("Yes")
else:
    print("No")