h, w = gets.chomp.split(" ").map(&:to_i) sy, sx, gy, gx = gets.chomp.split(" ").map(&:to_i) one = [[0,1],[0,-1],[1,0],[-1,0],[0,2],[2,0],[-2,0],[0,-2]] maze = [] h.times do |i| b = gets.chomp.split("").map(&:to_i) maze << b end st = [[sy-1,sx-1]] arr = Array.new(h).map{Array.new(w,-1)} arr[sy-1][sx-1] = 0 ok = false until st == [] i,j = st.pop one.each do |y,x| yy = i + y xx = j + x next if (i+y < 0 || i+y > h-1) || (j+x < 0 || j+x > w-1) next if arr[yy][xx] != -1 if y.abs == 2 || x.abs == 2 if maze[yy][xx] == maze[i][j] if y.abs == 2 yyy = i + (y/2) if maze[yy][xx] > maze[yyy][xx] arr[yy][xx] += 1 st << [yy,xx] end else xxx = j + (x/2) if maze[yy][xx] > maze[yy][xxx] arr[yy][xx] += 1 st << [yy,xx] end end end else if (maze[yy][xx] - maze[i][j]).abs <= 1 arr[yy][xx] += 1 st << [yy,xx] end end end if arr[gy-1][gx-1] == 0 ok = true break end # p st end if ok puts "YES" else puts "NO" end