#include using namespace std; #define rep(i,n) for(int i = 0; i < (n);i++) #define sz(x) int(x.size()) typedef long long ll; typedef pair P; constexpr int INF = 1e9; int dx[] = {1,0,-1,0}; int dy[] = {0,1,0,-1}; int main() { int r, c; cin >> r >> c; int sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy; sx--; sy--; gx--; gy--; vector g(r); rep(i,r) cin >> g[i]; queue

que; vector> d(r, vector(c,INF)); que.push(make_pair(sx, sy)); d[sx][sy] = 0; while (!que.empty()) { auto p = que.front(); que.pop(); for (int i = 0; i < 4; i++) { int x = p.first + dx[i], y = p.second + dy[i]; if (x < 0 || y < 0 || x >= r || y >= c) continue; if (g[x][y] == '#') continue; if (d[x][y] != INF) continue; que.push(make_pair(x, y)); d[x][y] = d[p.first][p.second] + 1; } } cout << d[gx][gy] << endl; return 0; }