#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define llint long long #define inf 1e18 #define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++) #define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++) #define chmin(x, y) (x) = min((x), (y)) #define chmax(x, y) (x) = max((x), (y)) using namespace std; typedef pair P; llint h, w; llint sx, sy, gx, gy; char c[1005][1005]; llint dist[1005][1005]; int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; void bfs() { queue

Q; Q.push(P(sx, sy)); for(int y = 1; y <= h; y++){ for(int x = 1; x <= w; x++){ dist[x][y] = inf; } } dist[sx][sy] = 0; llint x, y; while(Q.size()){ x = Q.front().first, y = Q.front().second; Q.pop(); for(int i = 0; i < 4; i++){ llint nx = x + dx[i], ny = y + dy[i]; if(nx < 1 || nx > w || ny < 1 || ny > h) continue; //if(c[nx][ny] == '#') continue; if(dist[nx][ny] < inf/2) continue; dist[nx][ny] = dist[x][y] + 1; Q.push(P(nx, ny)); } } } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> h >> w; cin >> sy >> sx >> gy >> gx; for(int y = 1; y <= h; y++){ for(int x = 1; x <= w; x++){ cin >> c[x][y]; } } bfs(); cout << dist[gx][gy] << endl; return 0; }