Problem p03361 (python)

Fixed submission:
hw = [int(i) for i in input().split()]
h = hw[0]
w = hw[1]
rows = []
possible = True
for i in range(h):
  rows.append(input())
i = 0
if h==1 and w==1:
  if rows[0]=='#':
    possible = False
elif h==1 and w>1:
  i = 0
  while i<w:
    if rows[0][i]=='#':
      if i==0:
        if rows[0][i+1]=='.':
          possible = False
          break
      elif i==w-1:
        if rows[0][i-1]=='.':
          possible = False
          break
      else:
        if rows[0][i+1]=='.' and rows[0][i-1]=='.':
          possible = False
          break
    i+=1
elif h>1 and w==1:
  i = 0
  while i<h:
    if rows[i][0]=='#':
      if i==0:
        if rows[i+1][0]=='.':
          possible = False
          break
      elif i==h-1:
        if rows[i-1][0]=='.':
          possible = False
          break
      else:
        if rows[i+1][0]=='.' and rows[i-1][0]=='.':
          possible = False
          break
    i+=1
else:
  while i<h:
    j = 0
    while j<w:
      if rows[i][j]=='#':
        if i==0 and j==0:
          if rows[i][j+1]=='.' and rows[i+1][j]=='.':
            possible=False
            break
        elif i==0 and j==w-1:
          if rows[i+1][j]=='.' and rows[i][j-1]=='.':
            possible = False
            break
        elif i==h-1 and j==0:
          if rows[i-1][j]=='.' and rows[i][j+1]=='.':
            possible = False
            break
        elif i==h-1 and j==w-1:
          if rows[i-1][j]=='.' and rows[i][j-1]=='.':
            possible = False
            break
        elif i==0 and j>0:
          if rows[i][j+1]=='.' and rows[i][j-1]=='.' and rows[i+1][j]=='.':
            possible=False
            break
        elif i>0 and j==0:
          if rows[i+1][j]=='.' and rows[i-1][j]=='.' and rows[i][j+1]=='.':
            possible = False
            break
        elif i>0 and j==w-1:
          if rows[i+1][j]=='.' and rows[i-1][j]=='.' and rows[i][j-1]=='.':
            possible = False
            break
        elif i==h-1 and j>0:
          if rows[i][j+1]=='.' and rows[i-1][j]=='.' and rows[i][j-1]=='.':
            possible = False
            break
        else:
          if rows[i+1][j]=='.' and rows[i-1][j]=='.' and rows[i][j+1]=='.' and rows[i][j-1]=='.':
            possible = False
            break
      j+=1
    if possible==False:
      break
    i+=1
if possible==False:
  print("No")
else:
  print("Yes")

Buggy submission (verdict: Runtime Error):
hw = [int(i) for i in input().split()]
h = hw[0]
w = hw[1]
rows = []
possible = True
for i in range(h):
  rows.append(input())
i = 0
if h==1 and w==1:
  if rows[0]=='#':
    possible = False
elif h==1 and w>1:
  i = 0
  while i<w:
    if rows[0][i]=='#':
      if i==0:
        if rows[0][i+1]=='.':
          possible = False
          break
      elif i==w-1:
        if rows[0][i-1]=='.':
          possible = False
          break
      else:
        if rows[0][i+1]=='.' and rows[0][i-1]=='.':
          possible = False
          break
    i+=1
elif h>1 and w==1:
  i = 0
  while i<h:
    if rows[i][0]=='#':
      if i==0:
        if rows[i+1][0]=='.':
          possible = False
          break
      elif i==h-1:
        if rows[i-1][0]=='.':
          possible = False
          break
      else:
        if rows[i+1][0]=='.' and rows[i-1][0]=='.':
          possible = False
          break
    i+=1
else:
  while i<h:
    j = 0
    while j<w:
      if rows[i][j]=='#':
        if i==0 and j==0:
          if rows[i][j+1]=='.' and rows[i+1][j]=='.':
            possible=False
            break
        elif i==0 and j>0:
          if rows[i][j+1]=='.' and rows[i][j-1]=='.' and rows[i+1][j]=='.':
            possible=False
            break
        elif i>0 and j==0:
          if rows[i+1][j]=='.' and rows[i-1][j]=='.' and rows[i][j+1]=='.':
            possible = False
            break
        elif i>0 and j==w-1:
          if rows[i+1][j]=='.' and rows[i-1][j]=='.' and rows[i][j-1]=='.':
            possible = False
            break
        elif i==h-1 and j>0:
          if rows[i][j+1]=='.' and rows[i-1][j]=='.' and rows[i][j-1]=='.':
            possible = False
            break
        elif i==h-1 and j==0:
          if rows[i-1][j]=='.' and rows[i][j+1]=='.':
            possible = False
            break
        elif i==0 and j==w-1:
          if rows[i+1][j]=='.' and rows[i][j-1]=='.':
            possible = False
            break
        elif i==h-1 and j==w-1:
          if rows[i-1][j]=='.' and rows[i][j-1]=='.':
            possible = False
            break
        else:
          if rows[i+1][j]=='.' and rows[i-1][j]=='.' and rows[i][j+1]=='.' and rows[i][j-1]=='.':
            possible = False
            break
      j+=1
    if possible==False:
      break
    i+=1
if possible==False:
  print("No")
else:
  print("Yes")

Buggy submission (verdict: Runtime Error):
hw = [int(i) for i in input().split()]
h = hw[0]
w = hw[1]
rows = []
possible = True
for i in range(h):
  rows.append(input())
i = 0
if h==1 and w==1:
  if rows[0]=='#':
    possible = False
elif h==1 and w>1:
  i = 0
  while i<w:
    if rows[0][i]=='#':
      if i==0:
        if rows[0][i+1]=='.':
          possible = False
          break
      elif i==w-1:
        if rows[0][i-1]=='.':
          possible = False
          break
      else:
        if rows[0][i+1]=='.' and rows[0][i-1]=='.':
          possible = False
          break
    i+=1
elif h>1 and w==1:
  i = 0
  while i<h:
    if rows[i][0]=='#':
      if i==0:
        if rows[i+1][0]=='.':
          possible = False
          break
      elif i==h-1:
        if rows[i-1][0]=='.':
          possible = False
          break
      else:
        if rows[i+1][0]=='.' and rows[i-1][0]=='.':
          possible = False
          break
    i+=1
else:
  while i<h:
    j = i
    while j<w:
      if rows[i][j]=='#':
        if i==0 and j==0:
          if rows[i][j+1]=='.' and rows[i+1][j]=='.':
            possible=False
            break
        elif i==0 and j>0:
          if rows[i][j+1]=='.' and rows[i][j-1]=='.' and rows[i+1][j]=='.':
            possible=False
            break
        elif i>0 and j==0:
          if rows[i+1][j]=='.' and rows[i-1][j]=='.' and rows[i][j+1]=='.':
            possible = False
            break
        elif i>0 and j==w-1:
          if rows[i+1][j]=='.' and rows[i-1][j]=='.' and rows[i][j-1]=='.':
            possible = False
            break
        elif i==h-1 and j>0:
          if rows[i][j+1]=='.' and rows[i-1][j]=='.' and rows[i][j-1]=='.':
            possible = False
            break
        elif i==h-1 and j==0:
          if rows[i-1][j]=='.' and rows[i][j+1]=='.':
            possible = False
            break
        elif i==0 and j==w-1:
          if rows[i+1][j]=='.' and rows[i][j-1]=='.':
            possible = False
            break
        elif i==h-1 and j==w-1:
          if rows[i-1][j]=='.' and rows[i][j-1]=='.':
            possible = False
            break
        else:
          if rows[i+1][j]=='.' and rows[i-1][j]=='.' and rows[i][j+1]=='.' and rows[i][j-1]=='.':
            possible = False
            break
      j+=1
    if possible==False:
      break
    i+=1
if possible==False:
  print("No")
else:
  print("Yes")