Problem p03361 (python)

Fixed submission:
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]

cnt = 0

if H >= 2 and W >= 3:
	if s[0][0] == "#" and s[0][1] == "." and s[1][0] == ".":
		cnt += 1

	for w in range(1, W - 1):
		if s[0][w] == "#" and s[0][w - 1] == "." and s[0][w + 1] == "." and s[1][w] == ".":
			cnt += 1

	if s[0][W - 1] == "#" and s[0][W - 2] == "." and s[1][W - 1] == ".":
		cnt += 1 

	if H >= 3:
		for h in range(1, H - 1):
			if s[h][0] == "#" and s[h][1] == "." and s[h + 1][0] == "." and s[h - 1][0] == ".":
				cnt += 1

			for w in range(1, W - 1):
				if s[h][w] == "#" and s[h][w - 1] == "." and s[h][w + 1] == "." and s[h + 1][w] == "." and s[h - 1][w] == ".":
					cnt += 1

			if s[h][W - 1] == "#" and s[h][W - 2] == "." and s[h + 1][W - 1] == "." and s[h - 1][W - 1] == ".":
				cnt += 1 

	if s[H - 1][0] == "#" and s[H - 1][1] == "." and s[H - 2][0] == ".":
		cnt += 1

	for w in range(1, W - 1):
		if s[H - 1][w] == "#" and s[H - 1][w - 1] == "." and s[H - 1][w + 1] == "." and s[H - 2][w] == ".":
			cnt += 1

	if s[H - 1][W - 1] == "#" and s[H - 1][W - 2] == "." and s[H - 2][W - 1] == ".":
		cnt += 1

elif H == 1:
	if s[0][0] == "#" and s[0][1] == ".":
		cnt += 1

	for w in range(1, W - 1):
		if s[0][w] == "#" and s[0][w - 1] == "." and s[0][w + 1] == ".":
			cnt += 1

	if s[0][W - 1] == "#" and s[0][W - 2] == ".":
		cnt += 1

elif W == 1:
	if s[0][0] == "#" and s[1][0] == ".":
		cnt += 1

	for h in range(1, H - 1):
		if s[h][0] == "#" and s[h - 1][0] == "." and s[h - 1][0] == ".":
			cnt += 1

	if s[H - 1][0] == "#" and s[H - 2][0] == ".":
		cnt += 1

else: #W==2
	if s[0][0] == "#" and s[0][1] == "." and s[1][0] == ".":
		cnt += 1

	if s[0][1] == "#" and s[0][0] == "." and s[1][1] == ".":
		cnt += 1

	if H >= 3:
		for h in range(1, H - 1):
			if s[h][0] == "#" and s[h][1] == "." and s[h + 1][0] == "." and s[h - 1][0] == ".":
				cnt += 1

			if s[h][1] == "#" and s[h][0] == "." and s[h + 1][1] == "." and s[h - 1][1] == ".":
				cnt += 1 

	if s[1][0] == "#" and s[1][1] == "." and s[0][0] == ".":
		cnt += 1

	if s[1][1] == "#" and s[1][0] == "." and s[0][1] == ".":
		cnt += 1


if cnt == 0:
	print("Yes")
else:
	print("No")

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
s = [list(input()) for _ in range(W)]

cnt = 0

if s[0][0] == "#" and s[0][1] == "." and s[1][0] == ".":
	cnt += 1

for w in range(1, W - 1):
	if s[0][w] == "#" and s[0][w - 1] == "." and s[0][w + 1] == "." and s[1][w] == ".":
		cnt += 1

if s[0][W - 1] == "#" and s[0][W - 2] == "." and s[1][W - 1] == ".":
	cnt += 1 

for h in range(1, H - 1):
	if s[h][0] == "#" and s[h][1] == "." and s[h + 1][0] == "." and s[h - 1][0] == ".":
		cnt += 1

	for w in range(1, W - 1):
		if s[h][w] == "#" and s[h][w - 1] == "." and s[h][w + 1] == "." and s[h + 1][w] == "." and s[h - 1][w] == ".":
			cnt += 1

	if s[h][W - 1] == "#" and s[h][W - 2] == "." and s[h + 1][W - 1] == "." and s[h - 1][W - 1] == ".":
		cnt += 1 

if s[H - 1][0] == "#" and s[H - 1][1] == "." and s[H - 2][0] == ".":
	cnt += 1

for w in range(1, W - 1):
	if s[H - 1][w] == "#" and s[H - 1][w - 1] == "." and s[H - 1][w + 1] == "." and s[H - 2][w] == ".":
		cnt += 1

if s[H - 1][W - 1] == "#" and s[H - 1][W - 2] == "." and s[H - 2][W - 1] == ".":
	cnt += 1

if cnt == 0:
	print("Yes")
else:
	print("No")