H, W = gets.split.map(&:to_i) sh, sw, gh, gw = gets.split.map{|x|x.to_i - 1} @b = Array.new(H) @memo = Array.new(H) @memo2 = Array.new(H) H.times{|i| @b[i] = gets.chomp.split('').map(&:to_i) @memo[i] = Array.new(W) @memo2[i] = Array.new(W, false) } @memo[gh][gw] = true @dw = [0, 0, 1, -1] @dh = [1, -1, 0, 0] def isInside w, h return w >= 0 && w < W && h >= 0 && h < H end def dfs noww, nowh if @memo[nowh][noww] != nil return @memo[nowh][noww] end @memo2[nowh][noww] = true 4.times{|i| w1 = @dw[i] + noww w2 = 2 * @dw[i] + noww h1 = @dh[i] + nowh h2 = 2 * @dh[i] + nowh if isInside(w1, h1) && !@memo2[h1][w1] && (@b[nowh][noww] - @b[h1][w1]).abs <= 1 if dfs(w1, h1) @memo[nowh][noww] = true return true end end if isInside(w2, h2) && !@memo2[h2][w2] if @b[nowh][noww] == @b[h2][w2] && @b[nowh][noww] > @b[h1][w1] if dfs(w2, h2) @memo[nowh][noww] = true return true end end end } @memo[nowh][noww] = false return false end puts dfs(sw, sh) ? "YES" : "NO"