Problem p03361 (python)

Fixed submission:
W, H = map(int, input().split())
a = []
for i in range(W):
    a.append(list(input()))
ans = "Yes"
for j in range(W):
  for k in range(H):
    if a[j][k] == "#":
       if j == 0:
          if k == 0:
           if a[j][k+1]  == "#":
              continue
           elif a[j+1][k]  == "#":
              continue
           else:
              ans = "No"
              break 
          elif  k == H-1:
           if a[j][k-1] == "#":
              continue
           elif a[j+1][k]  == "#":
              continue
           else:
              ans = "No"
              break 
       elif j == W-1:
          if k == 0:
           if a[j][k+1]  == "#":
              continue
           elif a[j-1][k]  == "#":
              continue
           else:
              ans = "No"
              break 
          elif  k == H-1:
           if a[j][k-1] == "#":
              continue
           elif a[j-1][k]  == "#":
              continue
           else:
              ans = "No"
              break 
          else:
           if a[j][k-1] == "#":
              continue
           elif a[j][k+1]  == "#":
              continue
           elif a[j-1][k]  == "#":
              continue
           else:
              ans = "No"
              break
       elif k == 0:
         if a[j][k+1]  == "#":
            continue
         elif a[j-1][k]  == "#":
            continue
         elif a[j+1][k]  == "#":
            continue
         else:
            ans = "No"
            break 
       elif k == H-1:
         if a[j][k-1] == "#":
            continue
         elif a[j-1][k]  == "#":
            continue
         elif a[j+1][k]  == "#":
            continue
         else:
            ans = "No"
            break           
       else:
           if a[j][k-1] == "#":
              continue
           elif a[j][k+1]  == "#":
              continue
           elif a[j-1][k]  == "#":
              continue
           elif a[j+1][k]  == "#":
              continue
           else:
              ans = "No"
              break 
print(ans)

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
a = []
for i in range(W):
    a.append(list(input()))
ans = "Yes"
for j in range(H):
  for k in range(W):
    if a[j][k] == "#":
       if j == 0:
          if k == 0:
           if a[j][k+1]  == "#":
              continue
           elif a[j+1][k]  == "#":
              continue
           else:
              ans = "No"
              break 
          elif  k == W-1:
           if a[j][k-1] == "#":
              continue
           elif a[j+1][k]  == "#":
              continue
           else:
              ans = "No"
              break 
       elif j == H-1:
          if k == 0:
           if a[j][k+1]  == "#":
              continue
           elif a[j-1][k]  == "#":
              continue
           else:
              ans = "No"
              break 
          elif  k == W-1:
           if a[j][k-1] == "#":
              continue
           elif a[j-1][k]  == "#":
              continue
           else:
              ans = "No"
              break 
          else:
           if a[j][k-1] == "#":
              continue
           elif a[j][k+1]  == "#":
              continue
           elif a[j-1][k]  == "#":
              continue
           else:
              ans = "No"
              break
       elif k == 0:
         if a[j][k+1]  == "#":
            continue
         elif a[j-1][k]  == "#":
            continue
         elif a[j+1][k]  == "#":
            continue
         else:
            ans = "No"
            break 
       elif k == W-1:
         if a[j][k-1] == "#":
            continue
         elif a[j-1][k]  == "#":
            continue
         elif a[j+1][k]  == "#":
            continue
         else:
            ans = "No"
            break           
       else:
           if a[j][k-1] == "#":
              continue
           elif a[j][k+1]  == "#":
              continue
           elif a[j-1][k]  == "#":
              continue
           elif a[j+1][k]  == "#":
              continue
           else:
              ans = "No"
              break 
print(ans)