Problem p03361 (python)

Fixed submission:
import sys

H, W = map(int, input().split())

A = []
for i in range(H):
    A.append(list(input()))

# print(A)
for r in range(H):
    for line in range(W):
        # print(r, line)
        if(A[r][line] != "#"):
            continue

        # print(r, line, A[r][line])

        if(line == 0 or W == 1):
            L = False
        else:
            L = A[r][line - 1] == "#"

        if(r == 0 or H == 1):
            U = False
        else:
            U = A[r - 1][line] == "#"

        if(line == W - 1 or W == 1):
            R = False
        else:
            R = A[r][line + 1] == "#"

        if(r == H - 1 or H == 1):
            D = False
        else:
            D = A[r + 1][line] == "#"

        # print(L, U, R, D)
        # print(L or U or R or D)
        if((L or U or R or D) is False):
            print("No")
            sys.exit()

print("Yes")

Buggy submission (verdict: Runtime Error):
import sys

H, W = map(int, input().split())

A = []
for i in range(W):
    A.append(list(input()))

# print(A)
for r in range(H):
    for line in range(W):
        # print(r, line)
        if(A[r][line] != "#"):
            continue

        # print(r, line, A[r][line])

        if(line == 0 or W == 1):
            L = False
        else:
            L = A[r][line - 1] == "#"

        if(r == 0 or H == 1):
            U = False
        else:
            U = A[r - 1][line] == "#"

        if(line == W - 1 or W == 1):
            R = False
        else:
            R = A[r][line + 1] == "#"

        if(r == H - 1 or H == 1):
            D = False
        else:
            D = A[r + 1][line] == "#"

        # print(L, U, R, D)
        # print(L or U or R or D)
        if((L or U or R or D) is False):
            print("No")
            sys.exit()

print("Yes")

Buggy submission (verdict: Runtime Error):
import sys

H, W = map(int, input().split())

A = []
for i in range(W):
    A.append(list(input()))

# print(A)
for r in range(H):
    for line in range(W):
        # print(r, line)
        if(A[r][line] != "#"):
            continue

        # print(r, line, A[r][line])

        if(line == 0):
            L = False
        else:
            L = A[r][line - 1] == "#"

        if(r == 0):
            U = False
        else:
            U = A[r - 1][line] == "#"

        if(line == W - 1):
            R = False
        else:
            R = A[r][line + 1] == "#"

        if(r == H - 1):
            D = False
        else:
            D = A[r + 1][line] == "#"

        # print(L, U, R, D)
        # print(L or U or R or D)
        if((L or U or R or D) is False):
            print("No")
            sys.exit()

print("Yes")

Buggy submission (verdict: Runtime Error):
import sys

H, W = map(int, input().split())

A = []
for i in range(W):
  A.append(input())

for r in range(H):
  for l in range(W):
    if(A[r][l] != "#"):
      continue
    
    if(l == 0):
      L = True
    else:
      L = A[r][l-1] == "#"
    
    if(r == 0):
      U = True
    else:
      U = A[r-1][l] == "#"
    
    if(l == W - 1):
      R = True
    else:
      R = A[r][l+1] == "#"
    
    if(r == H -1):
      D = True
    else:
      D = A[r+1][l] == "#"
    
    if(L or U or R or D is False):
      print("No")
      sys.exit()
      
print("Yes")