Problem p03361 (python)

Fixed submission:
import numpy as np
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
s = np.array(s)
out = "Yes"
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            continue
        if i == 0 and j == 0:
            up = s[i][j]
            down = s[i + 1][j]
            left = s[i][j]
            right = s[i][j + 1]
        elif i == 0 and j == w-1:
            up = s[i][j]
            down = s[i + 1][j]
            left = s[i][j - 1]
            right = s[i][j]
        elif i == h-1 and j == 0:
            up = s[i - 1][j]
            down = s[i][j]
            left = s[i][j]
            right = s[i][j + 1]
        elif i == h-1 and j == w-1:
            up = s[i - 1][j]
            down = s[i][j]
            left = s[i][j - 1]
            right = s[i][j]
        elif i == 0 and j != 0 and j != w-1:
            up = s[i][j]
            down = s[i + 1][j]
            left = s[i][j - 1]
            right = s[i][j + 1]
        elif i == h-1 and j != 0 and j != w-1:
            up = s[i - 1][j]
            down = s[i][j]
            left = s[i][j - 1]
            right = s[i][j + 1]
        elif j == 0 and i !=  0 and i != h-1:
            up = s[i - 1][j]
            down = s[i + 1][j]
            left = s[i][j]
            right = s[i][j + 1]
        elif j == w-1 and i != 0 and i!= h-1:
            up = s[i - 1][j]
            down = s[i + 1][j]
            left = s[i][j - 1]
            right = s[i][j]
        else:
            up = s[i-1][j]
            down = s[i+1][j]
            left = s[i][j-1]
            right = s[i][j+1]
        if s[i][j] != up and s[i][j] != down and s[i][j] != left and s[i][j] != right:
            out = "No"
print(out)

Buggy submission (verdict: Runtime Error):
def main():
    h, w = map(int, input().split())
    k = [["."]+list(input())+["."] for _ in range(h)]
    s = [[None]*(w+2) for _ in range(h+2)]
    s[0] = ["."]*(w+2)
    s[-1] = ["."]*(w+2)
    s[1:h+1] = k
    d = [[0, 1], [0, -1], [1, 0], [-1, 0]]
    if w == 1:
        for i in range(1, h+1):
            if s[i][1] == "#":
                if s[i+1][1] == "." and s[i-1][1] == ".":
                    print("No")
                    return  
        print("Yes")
        return
    if h == 1:
        for i in range(1, w+1):
            if s[1][i] == "#":
                if s[1][i+1] == "." and s[1][i-1] == ".":
                    print("No")
                    return  
        print("Yes")
        return
    for i in range(1, w+1):
        for j in range(1, h+1):
            if s[i][j] == ".":
                continue
            flag = True
            for (di, dj) in d:
                if s[i+di][j+dj] == "#":
                    flag = False
                    break
            if flag:
                print("No")
                return
    print("Yes")

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    h, w = map(int, input().split())
    k = [["."]+list(input())+["."] for _ in range(h)]
    s = [[None]*(w+2) for _ in range(h+2)]
    s[0] = ["."]*(w+2)
    s[-1] = ["."]*(w+2)
    s[1:h+1] = k
    d = [[0, 1], [0, -1], [1, 0], [-1, 0]]
    for i in range(1, w+1):
        for j in range(1, h+1):
            if s[i][j] == ".":
                continue
            flag = True
            for (di, dj) in d:
                if s[i+di][j+dj] == "#":
                    flag = False
                    break
            if flag:
                print("No")
                return
    print("Yes")

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import numpy as np
h, w = map(int, input().split())
s = np.array([list(input()) for _ in range(h)])
w1, w2 = np.array(["."]*w), np.array(["."]*(h+2))
s = np.hstack((w1, s))
s = np.hstack((s, w1))
s = np.vstack((w2.reshape(len(w2),1), s))
s = np.vstack((s, w2.reshape(len(w2),1)))
dx, dy = [1, 0, -1, 0], [0, 1, 0, -1]
for i in range(1, h+1, 1):
    for j in range(1, w+1, 1):
        now = s[i][j]
        c = 0
        if now == ".":
            continue
        for d in range(4):
            x = j + dx[d]
            y = i + dy[d]
            if now == s[y][x]:
                c += 1
        if c == 0:
            print("No")
            exit()
print("Yes")

Buggy submission (verdict: Runtime Error):
import numpy as np
h, w = map(int, input().split())
s = np.array([list(input()) for _ in range(h)])
w1, w2 = np.array(["."]*w), np.array(["."]*(h+2))
s = np.hstack((w1.reshape(len(w1),1), s))
s = np.hstack((s, w1.reshape(len(w1),1)))
s = np.vstack((w2, s))
s = np.vstack((s, w2))
dx, dy = [1, 0, -1, 0], [0, 1, 0, -1]
for i in range(1, h+1, 1):
    for j in range(1, w+1, 1):
        now = s[i][j]
        c = 0
        if now == ".":
            continue
        for d in range(4):
            x = j + dx[d]
            y = i + dy[d]
            if now == s[y][x]:
                c += 1
        if c == 0:
            print("No")
            exit()
print("Yes")