#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define IOS ios::sync_with_stdio(false); cin.tie(0); #define FOR(i, s, n) for(int i = (s), i##_len=(n); i < i##_len; ++i) #define FORS(i, s, n) for(int i = (s), i##_len=(n); i <= i##_len; ++i) #define VFOR(i, s, n) for(int i = (s); i < (n); ++i) #define VFORS(i, s, n) for(int i = (s); i <= (n); ++i) #define REP(i, n) FOR(i, 0, n) #define REPS(i, n) FORS(i, 0, n) #define VREP(i, n) VFOR(i, 0, n) #define VREPS(i, n) VFORS(i, 0, n) #define RFOR(i, s, n) for(int i = (s), i##_len=(n); i >= i##_len; --i) #define RFORS(i, s, n) for(int i = (s), i##_len=(n); i > i##_len; --i) #define RREP(i, n) RFOR(i, n, 0) #define RREPS(i, n) RFORS(i, n, 0) #define IREP(i, v) for(auto i = (v).begin(); i != (v).end(); ++i) #define IRREP(i, v) for(auto i = (v).rbegin(); i != (v).rend(); ++i) #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() #define SORT(v) sort(ALL(v)) #define RSORT(v) sort(RALL(v)) #define SZ(x) ((int)(x).size()) #define REV(x) reverse(ALL(x)) #define PB push_back #define EB emplace_back #define MP make_pair #define MT make_tuple #define BIT(n) (1LL<<(n)) #define UNIQUE(v) v.erase(unique(ALL(v)), v.end()) using ld = long double; using ll = long long; using ui = unsigned int; using ull = unsigned long long; using Pi_i = pair; using Pll_ll = pair; using VB = vector; using VC = vector; using VD = vector; using VI = vector; using VLL = vector; using VS = vector; using VSH = vector; using VULL = vector; const int MOD = 1000000007; const int INF = 1000000000; //1e9 const int NIL = -1; const ll LINF = 1000000000000000000; // 1e18 const double EPS = 1E-10; template bool chmax(T &a, const S &b){ if(a < b){ a = b; return true; } return false; } template bool chmin(T &a, const S &b){ if(b < a){ a = b; return true; } return false; } int main(){ int R, C, sx, sy, gx, gy; cin >> R >> C >> sx >> sy >> gx >> gy; --sx; --sy; --gx; --gy; vector Sq(R, VB(C)); REP(i, R){ string c; cin >> c; REP(j, C) Sq[i][j] = (c[j] == '#'); } vector dp(R, VI(C, INF)); dp[sy][sx] = 0; queue que; que.push({sy, sx}); while(!que.empty()){ int i(que.front().first), j(que.front().second); que.pop(); int dx[4] = {0, -1, 0, 1}; int dy[4] = {1, 0, -1, 0}; REP(a, 4){ int ni(i + dy[a]), nj(j + dx[a]); if(ni < 0 || R <= ni || nj < 0 || C <= nj || Sq[ni][nj] || dp[ni][nj] != INF) continue; if(ni == gy && nj == gx){ cout << dp[i][j] + 1 << endl; return 0; } dp[ni][nj] = dp[i][j] + 1; que.push({ni, nj}); } } return 0; }