#include #include #include #include #include #include using graph = std::vector>; const int INF = INT_MAX; const int NOT_SEARCHED = 100000; int s[2], g[2]; int h, w; const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; void bfs(graph &meiro){ std::queue> que; que.emplace(s[0], s[1]); meiro[s[0]][s[1]] = 0; while(!que.empty()){ std::pair now = que.front(); que.pop(); int y = now.first; int x = now.second; for(int i = 0; i < 4; i++){ int next_x = x + dx[i]; int next_y = y + dy[i]; if(next_x < 0 || next_x >= w) continue; if(next_y < 0 || next_y >= h) continue; if(meiro[next_y][next_x] != INF){ if(meiro[next_y][next_x] > meiro[y][x] + 1){ meiro[next_y][next_x] = meiro[y][x] + 1; que.emplace(next_y, next_x); } } } } } int main(){ std::cin >> h >> w; std::cin >> s[0] >> s[1]; std::cin >> g[0] >> g[1]; // to 0 origin s[0]--; s[1]--; g[0]--; g[1]--; graph meiro(h, std::vector(w, INF)); for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ char c; std::cin >> c; if(c == '.'){ meiro[i][j] = NOT_SEARCHED; } } } bfs(meiro); std::cout << meiro[g[0]][g[1]] << std::endl; return 0; }