#include using namespace std; int main() { int h, w; cin >> h >> w; vector> g(h+2, vector(w+2, 1)); vector> ans(h+2, vector(w+2, 1001001001)); vector> seen(h+2, vector(w+2, 0)); pair start, goal; cin >> start.first >> start.second; cin >> goal.first >> goal.second; for(int i=1;i<=h;i++) for(int j=1;j<=w;j++) { char c; cin >> c; if(c == '.') g[i][j] = 0; } if(g[start.first][start.second]){ cout << 0 << endl; return 0; } if(g[goal.first][goal.second]) { cout << 0 << endl; return 0; } queue> que; que.emplace(start); ans[start.first][start.second] = 0; while(!que.empty()) { pair p = que.front();que.pop(); if(seen[p.first][p.second]) continue; seen[p.first][p.second] = 1; int now = ans[p.first][p.second]; if(!g[p.first][p.second+1] && ans[p.first][p.second+1] > now + 1) { ans[p.first][p.second+1] = now + 1; que.emplace(make_pair(p.first, p.second+1)); } if(!g[p.first][p.second-1] && ans[p.first][p.second-1] > now + 1) { ans[p.first][p.second-1] = now + 1; que.emplace(make_pair(p.first, p.second-1)); } if(!g[p.first-1][p.second] && ans[p.first-1][p.second] > now + 1) { ans[p.first-1][p.second] = now + 1; que.emplace(make_pair(p.first-1, p.second)); } if(!g[p.first+1][p.second] && ans[p.first+1][p.second] > now + 1) { ans[p.first+1][p.second] = now + 1; que.emplace(make_pair(p.first+1, p.second)); } } if(ans[goal.first][goal.second] == 1001001001) cout << 0 << endl; else cout << ans[goal.first][goal.second] << endl; return 0; }