Problem p03361 (python)

Fixed submission:
h,w=map(int,input().split())
a=['.'*(w+2)]+['.'+input()+'.' for _ in range(h)]+['.'*(w+2)]
for i in range(h):
  for j in range(w):
    if a[i][j]=='#':
      if not(a[i+1][j]=='#' or a[i-1][j]=='#' or a[i][j+1]=='#' or a[i][j-1]=='#'):
        print('No')
        exit()
print('Yes')

Buggy submission (verdict: Runtime Error):
import sys
h,w=map(int,input().split())
dr=[input()+'.' for _ in range(h)]

if h==1 and w==1:
  print('Yes')
  sys.exit()

if dr[0][0]=='#':
  if dr[1][0]!='#' and dr[0][1]!='#':
    print('No')
    print(dr[1][0])
    sys.exit()
for j in range(1,w-1):
  if dr[0][j]=='#':
    if dr[0][j+1]!='#' and dr[1][j]!='#' and dr[0][j-1]!='#':
      print('No')
      sys.exit()
if dr[0][w-1]=='#':
  if dr[1][w-1]!='#' and dr[0][w-2]!='#':
    print('No')
    sys.exit()
for i in range(1,h-2):
  if dr[i][0]=='#':
    if dr[i+1][0]!='#' and dr[i][1]!='#' and dr[i-1][0]!='#':
      print('No')
      sys.exit()
  for i in range(1,w-1):
    if dr[i][j]=='#':
      if dr[i][j+1]!='#' and dr[i+1][j]!='#' and dr[i-1][j]!='#' and dr[i][j-1]!='#':
        print('No')
        sys.exit()
  if dr[i][w-1]=='#':
    if dr[i+1][w-1]!='#' and dr[i-1][w-1]!='#' and dr[i][w-2]!='#':
      print('N0')
      sys.exit()
if dr[h-1][0]=='#':
  if dr[h-1][1]!='#' and dr[h-2][0]!='#':
    print('N0')
    sys.exit()
for j in range(1,w-1):
  if dr[h-1][j]=='#':
    if dr[h-1][j+1]!='#' and dr[h-1][j-1]!='#' and dr[h-2][j]!='#':
      print('No')
      sys.exit()
if dr[h-1][w-1]=='#':
  if dr[h-1][w-2]!='#' and dr[h-2][w-1]!='#':
    print('N0')
    sys.exit()
print('Yes')

Buggy submission (verdict: Runtime Error):
import sys
h,w=map(int,input().split())
dr=[input()+'.' for _ in range(h)]
dr.append('.'*h)

if h==1 and w==1:
  print('Yes')
  sys.exit()

if dr[0][0]=='#':
  if dr[1][0]!='#' and dr[0][1]!='#':
    print('No')
    sys.exit()
for j in range(1,w-1):
  if dr[0][j]=='#':
    if dr[0][j+1]!='#' and dr[1][j]!='#' and dr[0][j-1]!='#':
      print('No')
      sys.exit()
if dr[0][w-1]=='#':
  if dr[1][w-1]!='#' and dr[0][w-2]!='#':
    print('No')
    sys.exit()
for i in range(1,h-2):
  if dr[i][0]=='#':
    if dr[i+1][0]!='#' and dr[i][1]!='#' and dr[i-1][0]!='#':
      print('No')
      sys.exit()
  for i in range(1,w-1):
    if dr[i][j]=='#':
      if dr[i][j+1]!='#' and dr[i+1][j]!='#' and dr[i-1][j]!='#' and dr[i][j-1]!='#':
        print('No')
        sys.exit()
  if dr[i][w-1]=='#':
    if dr[i+1][w-1]!='#' and dr[i-1][w-1]!='#' and dr[i][w-2]!='#':
      print('N0')
      sys.exit()
if dr[h-1][0]=='#':
  if dr[h-1][1]!='#' and dr[h-2][0]!='#':
    print('N0')
    sys.exit()
for j in range(1,w-1):
  if dr[h-1][j]=='#':
    if dr[h-1][j+1]!='#' and dr[h-1][j-1]!='#' and dr[h-2][j]!='#':
      print('No')
      sys.exit()
if dr[h-1][w-1]=='#':
  if dr[h-1][w-2]!='#' and dr[h-2][w-1]!='#':
    print('N0')
    sys.exit()
print('Yes')

Buggy submission (verdict: Runtime Error):
import sys
h,w=map(int,input().split())
dr=[input()+'.' for _ in range(h)]
dr.append('.'*h)

print(dr)

if h==1 and w==1:
  print('Yes')
  sys.exit()

if dr[0][0]=='#':
  if dr[1][0]!='#' and dr[0][1]!='#':
    print('No')
    sys.exit()
for j in range(1,w-1):
  if dr[0][j]=='#':
    if dr[0][j+1]!='#' and dr[1][j]!='#' and dr[0][j-1]!='#':
      print('No')
      sys.exit()
if dr[0][w-1]=='#':
  if dr[1][w-1]!='#' and dr[0][w-2]!='#':
    print('No')
    sys.exit()
for i in range(1,h-2):
  if dr[i][0]=='#':
    if dr[i+1][0]!='#' and dr[i][1]!='#' and dr[i-1][0]!='#':
      print('No')
      sys.exit()
  for i in range(1,w-1):
    if dr[i][j]=='#':
      if dr[i][j+1]!='#' and dr[i+1][j]!='#' and dr[i-1][j]!='#' and dr[i][j-1]!='#':
        print('No')
        sys.exit()
  if dr[i][w-1]=='#':
    if dr[i+1][w-1]!='#' and dr[i-1][w-1]!='#' and dr[i][w-2]!='#':
      print('N0')
      sys.exit()
if dr[h-1][0]=='#':
  if dr[h-1][1]!='#' and dr[h-2][0]!='#':
    print('N0')
    sys.exit()
for j in range(1,w-1):
  if dr[h-1][j]=='#':
    if dr[h-1][j+1]!='#' and dr[h-1][j-1]!='#' and dr[h-2][j]!='#':
      print('No')
      sys.exit()
if dr[h-1][w-1]=='#':
  if dr[h-1][w-2]!='#' and dr[h-2][w-1]!='#':
    print('N0')
    sys.exit()
print('Yes')

Buggy submission (verdict: Runtime Error):
import sys
h,w=map(int,input().split())
dr=[input()+'.' for _ in range(h)]
dr.append('.'*h)
frag='Yes'

if h==1 and w==1:
  print('Yes')
  sys.exit()

if dr[0][0]=='#':
  if dr[1][0]!='#' and dr[0][1]!='#':
    print('No')
    sys.exit()
for j in range(1,w-1):
  if dr[0][j]=='#':
    if dr[0][j+1]!='#' and dr[1][j]!='#' and dr[0][j-1]!='#':
      print('No')
      sys.exit()
if dr[0][w-1]=='#':
  if dr[1][w-1]!='#' and dr[0][w-2]!='#':
    print('No')
    sys.exit()
for i in range(1,h-2):
  if dr[i][0]=='#':
    if dr[i+1][0]!='#' and dr[i][1]!='#' and dr[i-1][0]!='#':
      print('No')
      sys.exit()
  for i in range(1,w-1):
    if dr[i][j]=='#':
      if dr[i][j+1]!='#' and dr[i+1][j]!='#' and dr[i-1][j]!='#' and dr[i][j-1]!='#':
        print('No')
        sys.exit()
  if dr[i][w-1]=='#':
    if dr[i+1][w-1]!='#' and dr[i-1][w-1]!='#' and dr[i][w-2]!='#':
      print('N0')
      sys.exit()
for j in range(w-1):
  if dr[h-1][j]=='#':
    if dr[h-1][j+1]!='#' and dr[h-1][j-1]!='#' and dr[h-2][j]!='#':
      print('No')
      sys.exit()
if dr[h-1][w-1]=='#':
  if dr[h-1][w-2]!='#' and dr[h-2][w-1]!='#':
    print('N0')
    sys.exit()
print('Yes')

Buggy submission (verdict: Runtime Error):
import sys
h,w=map(int,input().split())
dr=[input() for _ in range(h)]
frag='Yes'

for j in range(w-1):
  if dr[0][j]=='#':
    if dr[0][j+1]!='#' and dr[1][j]!='#' and dr[0][j-1]!='#':
      print('No')
      sys.exit()
if dr[0][w-1]=='#':
  if dr[1][w-1]!='#' and dr[0][w-2]!='#':
    print('No')
    sys.exit()
for i in range(h-2):
  for i in range(w-1):
    if dr[i][j]=='#':
      if dr[i][j+1]!='#' and dr[i+1][j]!='#' and dr[i-1][j]!='#' and dr[i][j-1]!='#':
        print('No')
        sys.exit()
  if dr[i][w-1]=='#':
    if dr[i+1][w-1]!='#' and dr[i-1][w-1]!='#' and dr[i][w-2]!='#':
      print('N0')
      sys.exit()
for j in range(w-1):
  if dr[h-1][j]=='#':
    if dr[h-1][j+1]!='#' and dr[h-1][j-1]!='#' and dr[h-2][j]!='#':
      print('No')
      sys.exit()
if dr[h-1][w-1]=='#':
  if dr[h-1][w-2]!='#' and dr[h-2][w-1]!='#':
    print('N0')
    sys.exit()
print('Yes')