output_description: Print the minimum number of times the magician needs to use the magic. If he
  cannot reach (D_h,D_w), print `-1` instead.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      H W
      C_h C_w
      D_h D_w
      S_{11}\ldots S_{1W}
      \vdots
      S_{H1}\ldots S_{HW}
attempt: def get_position(node):
      h = node // W + 1
      w = node % W
      if w == 0:
          w = W
      return (h, w)
  
  
  H, W = list(map(int, input().split()))
  C_h, C_w = list(map(int, input().split()))
  D_h, D_w = list(map(int, input().split()))
  
  N = H * W
  
  # 壁のノード
  wall = []
  for h in range(H):
      for w_idx, w in enumerate(list(input())):
          if w == "#":
              wall.append(W * h + w_idx + 1)
  # 道のノード
  path = [_ for _ in range(1, N + 1) if _ not in wall]
  
  # 隣接リスト
  ad = {}
  for n in range(N):
      ad[n + 1] = []
  
  for n in range(N):
      n = n + 1
      if n not in wall:
          up = n - W
          if up > 0 and up not in wall:
              ad[n].append(up)
  
          down = n + W
          if down <= N and down not in wall:
              ad[n].append(down)
  
          left = n - 1
          if n % W != 1 and left not in wall and left > 0:
              ad[n].append(left)
  
          right = n + 1
          if n % W != 0 and right not in wall:
              ad[n].append(right)
  
  from collections import deque
  
  color = {}
  for n in range(N):
      color[n + 1] = -1
  
  depth = {}
  for n in range(N):
      depth[n + 1] = -1
  
  group = {}
  # for n in range(N):
  #     group[n+1] = -1
  
  group_ = 0
  for p in path:
      if color[p] != -1:
          continue
      elif color[p] == -1:
          start = p
          color[start] = 0
          que = deque([start])
          visit = deque([])
          group_ += 1
          group[group_] = [start]
  
      depth[start] = 0
      while len(que) > 0:
          start = que[0]
          #         print(start,que)
          for v in ad[start]:
              #             print(v,color[v])
              if color[v] == -1:
                  que.append(v)
                  color[v] = 0
                  depth[v] = depth[start] + 1
                  group[group_].append(v)
  
          color[start] = 1
          visit.append(que.popleft())
  
  group_conect = {}
  for g in group.keys():
      group_conect[g] = set([])
  
  break_flag = False
  for g_1 in range(1, group_ + 1):
      for g_2 in range(g_1 + 1, group_ + 1):
          for g1 in group[g_1]:
              for g2 in group[g_2]:
                  y, x = get_position(g1)
                  y_min = max(0, y - 2)
                  y_max = min(W, y + 2)
                  x_min = max(0, x - 2)
                  x_max = min(H, x + 2)
                  Y, X = get_position(g2)
  
                  if y_min <= Y and Y <= y_max and x_min <= X and X <= x_max:
                      group_conect[g_1].add(g_2)
                      group_conect[g_2].add(g_1)
                      break
  print(group_conect)
  
problem_description: Statement
  
  A maze is composed of a grid of H \times W squares - H vertical, W horizontal.
  
  The square at the i-th row from the top and the j-th column from the left -
  (i,j) \- is a wall if S_{ij} is `#` and a road if S_{ij} is `.`.
  
  There is a magician in (C_h,C_w). He can do the following two kinds of moves:
  
    * Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.
    * Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in.
  
  In either case, he cannot go out of the maze.
  
  At least how many times does he need to use the magic to reach (D_h, D_w)?
samples: [{"input": "4 4\n    1 1\n    4 4\n    ..#.\n    ..#.\n    .#..\n    .#..", "output": "1\n    \n\nFor example, by walking to (2,2) and then using the magic to travel to (4,4),\njust one use of magic is enough.\n\nNote that he cannot walk diagonally.\n\n* * *"}, {"input": "4 4\n    1 4\n    4 1\n    .##.\n    ####\n    ####\n    .##.", "output": "-1\n    \n\nHe cannot move from there.\n\n* * *"}, {"input": "4 4\n    2 2\n    3 3\n    ....\n    ....\n    ....\n    ....", "output": "0\n    \n\nNo use of magic is needed.\n\n* * *"}, {"input": "4 5\n    1 2\n    2 5\n    #.###\n    ####.\n    #..##\n    #..##", "output": "2"}]