Problem p03361 (python)

Fixed submission:
h, w = map(int, input().split())
s = [input() for _ in range(h)]
dx = [-1, 0, 0, 1]
dy = [0, -1, 1, 0]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            flag = 0
            for k in range(4):
                ny = i + dy[k]
                nx = j + dx[k]
                if nx < 0 or w <= nx:
                    continue
                if ny < 0 or h <= ny:
                    continue
                if s[ny][nx] == '#':
                    flag = 1
            if flag == 0:
                print('No')
                exit()
else:
    print('Yes')

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
s = [input() for _ in range(h)]
dx = [-1, 0, 0, 1]
dy = [0, -1, 1, 0]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            flag = 0
            for k in range(4):
                nx = i + dx[k]
                ny = j + dy[k]
                if nx < 0 or w <= nx:
                    continue
                if ny < 0 or h <= ny:
                    continue
                if s[nx][ny] == '#':
                    flag = 1
            if flag == 0:
                print('No')
                exit()
else:
    print('Yes')

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
s = [input() for _ in range(w)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            flag = 0
            L = []
            if i == 0 and j == 0:
                L.extend([(0, 1), (1, 0)])
            elif i == 0 and j == w - 1:
                L.extend([(-1, 0), (1, 0)])
            elif i == h - 1 and j == 0:
                L.extend([(-1, 0), (0, 1)])
            elif i == h - 1 and j == w - 1:
                L.extend([(-1, 0), (0, -1)])
            elif i == 0:
                L.extend([(1, 0), (0, -1), (0, 1)])
            elif i == h - 1:
                L.extend([(-1, 0), (0, 1), (0, -1)])
            elif j == 0:
                L.extend([(-1, 0), (1, 0), (0, 1)])
            elif j == w - 1:
                L.extend([(-1, 0), (1, 0), (0, -1)])
            else:
                L.extend([(-1, 0), (1, 0), (0, -1), (0, 1)])
            
            for dx, dy in L:
                    if s[i + dx][j + dy] == '#':
                        flag = 1
            if flag == 0:
                print('No')
                exit()
else:
    print('Yes')

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
s = [input() for _ in range(w)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            flag = 0
            L = []
            if i == 0 and j == 0:
                L.extend([(0, 1), (1, 0)])
            elif i == 0 and j == w - 1:
                L.extend([(-1, 0), (1, 0)])
            elif i == h - 1 and j == 0:
                L.extend([(1, 0), (0, 1)])
            elif i == h - 1 and j == w - 1:
                L.extend([(-1, 0), (0, -1)])
            elif i == 0:
                L.extend([(1, 0), (0, -1), (0, 1)])
            elif i == h - 1:
                L.extend([(-1, 0), (0, 1), (0, -1)])
            elif j == 0:
                L.extend([(-1, 0), (1, 0), (0, 1)])
            elif j == w - 1:
                L.extend([(-1, 0), (1, 0), (0, -1)])
            else:
                L.extend([(-1, 0), (1, 0), (0, -1), (0, 1)])
            
            for dx, dy in L:
                    if s[i + dx][j + dy] == '#':
                        flag = 1
            if flag == 0:
                print('No')
                exit()
else:
    print('Yes')