#include using namespace std; using PP = pair; using TT = pair; int h, w; int si, sj; int gi, gj; char s[1004][1004]; int main() { cin >> h >> w >> si >> sj >> gi >> gj; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { cin >> s[i + 1][j + 1]; } } h += 2; w += 2; const int di[] = { 0, 1, 0, -1 }; const int dj[] = { 1, 0, -1, 0 }; queue que; que.push(TT(0, PP(si, sj))); while (!que.empty()) { TT t = que.front(); que.pop(); int d = t.first; int i = t.second.first; int j = t.second.second; if (i < 0 || i >= h) continue; if (j < 0 || j >= w) continue; if (s[i][j] == '#') continue; if (i == gi && j == gj) { cout << d << endl; return 0; } for (int k = 0; k < 4; ++k) { que.push(TT(d + 1, PP(i + di[k], j + dj[k]))); } } }