class UnionFind def initialize(n) @size = Array.new(n, 1) @rank = Array.new(n, 0) @parent = [] (0..n).each do |i| @parent[i] = i end end def find(x) if @parent[x] == x x else @parent[x] = find(@parent[x]) end end def unite(x, y) x = find(x) y = find(y) return if x == y if @rank[x] < @rank[y] @parent[x] = y @size[y] += @size[x] else @parent[y] = x @size[x] += @size[y] @rank[x] += 1 if @rank[x] == @rank[y] end end def same?(x, y) find(x) == find(y) end def size(x) @size[find(x)] end end H, W = gets.split.map(&:to_i) SY, SX, GY, GX = gets.split.map(&:to_i) B = H.times.map { gets.chomp.chars.map(&:to_i) } uf = UnionFind.new(10_000) def calc_z(y, x) y * W + x end DY = [-1, 0, 1, 0, -2, 0, 2, 0] DX = [0, 1, 0, -1, 0, 2, 0, -2] H.times do |y| W.times do |x| h = B[y][x] z = calc_z(y, x) 8.times do |i| ny = y + DY[i] nx = x + DX[i] nz = calc_z(ny, nx) next if ny < 0 || nx < 0 || H <= ny || W <= nx next if uf.same?(z, nz) nh = B[ny][nx] if i < 4 d = (h - nh).abs next if d >= 2 uf.unite(z, nz) else next if h != nh nny = y + DY[i - 4] nnx = x + DX[i - 4] nnz = calc_z(nny, nnx) nnh = B[nny][nnx] next if h <= nnh uf.unite(z, nz) end end end end if uf.same?(calc_z(SY - 1, SX - 1), calc_z(GY - 1, GX - 1)) puts 'YES' else puts 'NO' end