#include #define rep(i, n) for (ll i = 0; i < n; ++i) typedef long long ll; using namespace std; const int INF = 1e9; int main() { int h, w; cin >> h >> w; int sy, sx, gy, gx; cin >> sy >> sx >> gy >> gx; sy--, sx--, gy--, gx--; vector s(h); rep(i, h) cin >> s[i]; vector> d(h, vector(w, INF)); queue> que; que.push({sy, sx}); d[sy][sx] = 0; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; while (!que.empty()) { auto now = que.front(); que.pop(); rep(i, 4) { int ny = now.first + dy[i]; int nx = now.second + dx[i]; if (ny >= 0 && nx >= 0 && ny < h && nx < w && s[ny][nx] == '.' && d[ny][nx] == INF) { d[ny][nx] = d[now.first][now.second] + 1; que.push({ny, nx}); } } } cout << d[gy][gx] << endl; return 0; }