Problem p03361 (python)

Fixed submission:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

h,w = MAP()
s = [STR() for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            flag = [False for _ in range(4)]
            if i > 0 and s[i-1][j] == '#':
                flag[0] = True
            if i < h - 1 and s[i+1][j] == '#':
                flag[1] = True
            if j > 0 and s[i][j-1] == '#':
                flag[2] = True
            if j < w - 1 and s[i][j+1] == '#':
                flag[3] = True
            if sum(flag) == 0:
                print('No')
                exit()
print('Yes')

Buggy submission (verdict: Runtime Error):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

h,w = MAP()
s = [STR() for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            flag = [False for _ in range(4)]
            if i > 0 and s[i-1][j] == '#':
                flag[0] = True
            if i < w - 1 and s[i+1][j] == '#':
                flag[1] = True
            if j > 0 and s[i][j-1] == '#':
                flag[2] = True
            if j < h - 1 and s[i][j+1] == '#':
                flag[3] = True
            if sum(flag) == 0:
                print('No')
                exit()
print('Yes')

Buggy submission (verdict: Runtime Error):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

h,w = MAP()
s = [STR() for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            flag = [False for _ in range(4)]
            if i > 0 and s[i-1][j] == '#':
                flag[0] = True
            if i < w - 1 and s[i+1][j] == '#':
                flag[1] = True
            if j > 0 and s[i][j-1] == '#':
                flag[2] = True
            if j < h - 1 and s[i][j+1] == '#':
                flag[3] = True
            if sum(flag) <= 0:
                print('No')
                exit()
            elif sum(flag) == 1:
                index = flag.index(True)
                a = 0
                b = 0
                if index == 0:
                    a = i - 1
                    b = j
                elif index == 1:
                    a = i + 1
                    b = j
                elif index == 2:
                    a = i
                    b = j - 1
                else:
                    a = i
                    b = j + 1
                if a > 0 and s[a-1][b] == '#':
                    flag[0] = True
                if a < w - 1 and s[a+1][b] == '#':
                    flag[1] = True
                if b > 0 and s[a][b-1] == '#':
                    flag[2] = True
                if b < h - 1 and s[a][b+1] == '#':
                    flag[3] = True
                if sum(flag) < 2:
                    print('No')
                    exit()
print('Yes')