#include using namespace std; #define ALL(a) (a).begin(),(a).end() #define rALL(a) (a).rbegin(),(a).rend() typedef pair Pint; typedef pair Pll; typedef int64_t ll; int H, W, a, b; vector dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1}; int bfs(int x, int y, vector> &A){ queue Next; Next.push(Pint(x, y)); vector> dist(H, vector (W, 1000000007)); dist.at(x).at(y) = 0; while(Next.size() != 0){ Pint p = Next.front(); Next.pop(); for(int i = 0; i < 4; i++){ int nx = p.first + dx.at(i), ny = p.second + dy.at(i); if (nx < H && 0 <= nx && ny < W && 0 <= ny && dist.at(nx).at(ny) == 1000000007 && A.at(nx).at(ny) == '.'){ Next.push(Pint(nx, ny)); dist.at(nx).at(ny) = dist.at(p.first).at(p.second) + 1; } } } int value = dist.at(a - 1).at(b - 1); return value; } int main() { cin >> H >> W; int x, y; cin >> x >> y >> a >> b; vector> A(H, vector(W)); for (int i = 0; i < H; i++){ for (int j = 0; j < W; j++){ cin >> A.at(i).at(j); } } cout << bfs(x - 1, y - 1, A) << endl; }