#include #define rep(i,n) for (int i = 0; i < (n); ++i) #define all(x) (x).begin(),(x).end() using namespace std; using ll = long long; using P = pair; template void chmin(T &a, const T &b) noexcept { if (b < a) a = b; } template void chmax(T &a, const T &b) noexcept { if (a < b) a = b; } void debug_out() { cout << "\n"; } template void debug_out(const T &x, const Args &... args) { cout << x << " "; debug_out(args...);} #ifdef _DEBUG #define debug(...) debug_out(__VA_ARGS__) #else #define debug(...) #endif /* delta(V v, update(V t)) index(V v) -> int */ template vector bfs(int n, V s, Delta delta, Index index) { vector dist(n,-1); queue que; dist[index(s)] = 0; que.push(s); while(!que.empty()) { V v = que.front(); que.pop(); auto update = [&](V t) { if (dist[index(t)] == -1) { dist[index(t)] = dist[index(v)]+1; que.push(t); } }; delta(v,update); } return dist; } template struct grid_delta { int h,w; F caller; const int dx[8] = {-1,0,1,0,-1,1,-1,1}; const int dy[8] = {0,-1,0,1,-1,-1,1,1}; grid_delta(int h, int w, F f):h(h),w(w),caller(f){} template void operator()(P v, G update) { int y = v.first, x = v.second; for (int i=0; i<4; ++i) { int ny = y+dy[i], nx = x+dx[i]; if (nx<0 || ny<0 || nx>=w || ny>=h) continue; caller(v, P(ny,nx), update); } } }; template grid_delta make_grid_delta(int h, int w, F f) { return grid_delta(h, w, f);} struct grid_index { int h,w; grid_index(int h, int w):h(h),w(w){} int operator()(P v){ return v.first*w+v.second;} P inv(int ind){ return P((ind-(ind%w))/w ,ind%w);} }; int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(15); int h,w; cin >> h >> w; P st, go; cin >> st.first >> st.second >> go.first >> go.second; st.first--; st.second--; go.first--; go.second--; vector s(h); rep(i,h) cin >> s[i]; auto index = grid_index(h,w); // auto caller = [&](P v, P t, auto update) { // if (s[t.first][t.second] == '.') update(t); // }; auto caller = [&](P v, P t, auto update) { int now = s[v.first][v.second] - '0'; if (s[t.first][t.second]-'0' == now) update(t); else if (s[t.first][t.second]-'0' == now+1) update(t); else if (s[t.first][t.second]-'0' == now-1) update(t); else if (s[t.first][t.second]-'0' < now) { int ny = v.first + 2*(t.first-v.first); int nx = v.second + 2*(t.second-v.second); if (nx<0 || ny<0 || nx>=w || ny>=h) return; if (s[ny][nx]-'0' == now) update(P(ny,nx)); } }; auto delta = make_grid_delta(h, w, caller); auto res = bfs(h*w, st, delta, index); if (res[index(go)] == -1) cout << "NO" <