Problem p02579 (python)

Fixed submission:
from collections import deque
h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
maze = [[9]*(w+4)]
maze.append([9]*(w+4))
for i in range(h):
  s = input()
  tmp = [9,9]
  for i in s:
    if i == "#":
      tmp.append(9)
    else:
      tmp.append(0)
  tmp.append(9)
  tmp.append(9)
  maze.append(tmp)
maze.append([9]*(w+4))
maze.append([9]*(w+4))
#print(maze)

pos = deque()
pos.append([ch+1,cw+1,0])
while len(pos)>0:
  x, y, hop = pos.popleft()
  #print(pos)
  #print[x,y,hop)
  if (x,y) == (dh+1,dw+1):
    print(hop)
    break
  maze[x][y] = 3
  
  if maze[x-1][y] <3:
    pos.appendleft([x-1,y,hop])
    maze[x-1][y] = 3  
    
  if maze[x+1][y] <3:
    pos.appendleft([x+1,y,hop])
    maze[x+1][y] = 3
    
  if maze[x][y-1] <3:
    pos.appendleft([x,y-1,hop])
    maze[x][y-1] = 3
    
  if maze[x][y+1] <3:
    pos.appendleft([x,y+1,hop])
    maze[x][y+1] = 3

  move =[(-2,0),(-2,1),(-2,2),(-2,-1),(-2,-2),(2,0),(2,1),(2,2),(2,-1),(2,-2),(0,-2),(-1,-2),(1,-2),(0,2),(-1,2),(1,2),(-1,1),(-1,-1),(1,1),(1,-1)]
  for dx,dy in move:
    if maze[x+dx][y+dy] <2:
      pos.append([x+dx,y+dy,hop+1])
      maze[x+dx][y+dy] = 2
  
else:
  print(-1)

Buggy submission (verdict: Runtime Error):
from collections import deque
h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
maze = [[9]*(w+2)]
for i in range(h):
  s = input()
  tmp = [9]
  for i in s:
    if i == "#":
      tmp.append(9)
    else:
      tmp.append(0)
  tmp.append(9)
  maze.append(tmp)
maze.append([9]*(w+2))
#print(maze)

pos = deque()
pos.append([ch,cw,0])
while len(pos)>0:
  x, y, hop = pos.popleft(()
  #print(x,y,hop)
  if (x,y) == (dh,dw):
    print(hop)
    break
    
  maze[x][y] = 2
  
  flag = 0
  if maze[x-1][y] <2:
    pos.append([x-1,y,hop])
    flag = 1
  if maze[x+1][y] <2:
    pos.append([x+1,y,hop])
    flag = 1
  if maze[x][y-1] <2:
    pos.append([x,y-1,hop])
    flag = 1
  if maze[x][y+1] <2:
    pos.append([x,y+1,hop])
    flag = 1
    
  if flag == 0:
    try:
      if maze[x-2][y] <2:
        pos.append([x-2,y,hop+1])
    except:
      pass
    
    try:
      if maze[x-2][y+1] <2:
        pos.append([x-2,y+1,hop+1])
    except:
      pass
        
    try: 
      if maze[x-2][y+2] <2:
        pos.append([x-2,y+2,hop+1])
    except:
      pass
    
    try:
      if maze[x-2][y-1] <2:
        pos.append([x-2,y-1,hop+1])
    except:
      pass
    
    try:
      if maze[x-2][y-2] <2:
        pos.append([x-2,y-2,hop+1])
    except:
      pass
    
    try:
      if maze[x+2][y] <2:
        pos.append([x+2,y,hop+1])
    except:
      pass
    try:
      if maze[x+2][y-1] <2:
        pos.append([x+2,y-1,hop+1])
    except:
      pass
    try:
      if maze[x+2][y-2] <2:
        pos.append([x+2,y-2,hop+1])
    except:
      pass
    try:
      if maze[x+2][y+1] <2:
        pos.append([x+2,y+1,hop+1])
    except:
      pass
    
    try:
      if maze[x+2][y+2] <2:
        pos.append([x+2,y+2,hop+1])
    except:
      pass
    
    try:
      if maze[x][y-2] <2:
        pos.append([x,y-2,hop+1])
    except:
      pass
    
    try:
      if maze[x+1][y-2] <2:
        pos.append([x+1,y-2,hop+1])
    except:
      pass
    try:
      if maze[x-1][y-2] <2:
        pos.append([x-1,y-2,hop+1])
    except:
      pass
    
    try:
      if maze[x][y+2] <2:
        pos.append([x,y+2,hop+1])
    except:
      pass
    
    try:
      if maze[x+1][y+2] <2:
        pos.append([x+1,y+2,hop+1])
    except:
      pass
    
    try:
      if maze[x-1][y+2] <2:
        pos.append([x-1,y+2,hop+1])
    except:
      pass
    if maze[x-1][y-1] <2:
      pos.append([x-1,y-1,hop+1])
    if maze[x-1][y+1] <2:
      flag=1
      pos.append([x-1,y+1,hop+1])
    if maze[x+1][y-1] <2:
      flag=1
      pos.append([x+1,y-1,hop+1])
    if maze[x+1][y+1] <2:
      flag=1
      pos.append([x+1,y+1,hop+1])
    
else:
  print(-1)