Problem p03361 (python)

Fixed submission:
h, w = map(int, input().split())
l1 =[]

for i in range(h):
    l2 = list(input())
    l1.append(l2)

is_no = 0
for i in range(h):
    if is_no:
        break
    for j in range(w):
        if is_no:
            break
        if l1[i][j] == '#':
            if i == 0:
                if j == 0:
                    if l1[i][j + 1] == '.' and l1[i + 1][j] == '.':
                        is_no = 1
                        
                elif j == w - 1:
                    if l1[i][j - 1] == '.' and l1[i + 1][j] == '.':
                     is_no = 1

                else:
                    if l1[i][j - 1] == '.'  and l1[i][j + 1] == '.' and l1[i + 1][j] == '.':
                        is_no = 1


            elif i == h - 1:
                if j == 0:
                    if l1[i][j + 1] == '.' and l1[i - 1][j] == '.':
                        is_no = 1

                elif j == w - 1:
                    if l1[i][j - 1] == '.' and l1[i - 1][j] == '.':
                        is_no = 1

                else:
                    if l1[i][j - 1] == '.'  and l1[i][j + 1] == '.' and l1[i - 1][j] == '.':
                        is_no = 1


            else:
                if j == 0:
                    if l1[i][j + 1] == '.' and l1[i - 1][j] == '.' and l1[i + 1][j] == '.':
                        is_no = 1

                elif j == w - 1:
                    if l1[i][j - 1] == '.' and l1[i - 1][j] == '.' and l1[i + 1][j] == '.':
                        is_no = 1

                else:
                    if l1[i][j - 1] == '.'  and l1[i][j + 1] == '.' and l1[i - 1][j] == '.' and l1[i + 1][j] == '.':
                        is_no = 1


if is_no:
    print("No")
else:
    print("Yes")

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
l1 =[]

for i in range(h):
    l2 = list(input())
    l1.append(l2)

is_no = 0
for i in range(w):
    for j in range(h):
        if l1[i][j] == '#':
            if i == 0:
                if j == 0:
                    if l1[i][j + 1] == '.' and l1[i + 1][j] == '.':
                        is_no = 1
                        
                elif j == w - 1:
                    if l1[i][j - 1] == '.' and l1[i + 1][j] == '.':
                     is_no = 1

                else:
                    if l1[i][j - 1] == '.'  and l1[i][j + 1] == '.' and l1[i + 1][j] == '.':
                        is_no = 1


            elif i == h - 1:
                if j == 0:
                    if l1[i][j + 1] == '.' and l1[i - 1][j] == '.':
                        is_no = 1

                elif j == w - 1:
                    if l1[i][j - 1] == '.' and l1[i - 1][j] == '.':
                        is_no = 1

                else:
                    if l1[i][j - 1] == '.'  and l1[i][j + 1] == '.' and l1[i - 1][j] == '.':
                        is_no = 1


            else:
                if j == 0:
                    if l1[i][j + 1] == '.' and l1[i - 1][j] == '.' and l1[i + 1][j] == '.':
                        is_no = 1

                elif j == w - 1:
                    if l1[i][j - 1] == '.' and l1[i - 1][j] == '.' and l1[i + 1][j] == '.':
                        is_no = 1

                else:
                    if l1[i][j - 1] == '.'  and l1[i][j + 1] == '.' and l1[i - 1][j] == '.' and l1[i + 1][j] == '.':
                        is_no = 1


if is_no:
    print("No")
else:
    print("Yes")