#include <bits/stdc++.h>
using namespace std;
#define y1 aaaa
int n, m, y1, x1, y2, x2, visited[301][301], y, x, cnt = 1;
char a[301][301];
bool isEnd;
vector<pair<int, int>> v;
queue<pair<int,int>> q;
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};

void dfs(int y, int x){
    if(y == y2 - 1 && x == x2 - 1){
        isEnd = true;
        return;
    }
    
    for(int i = 0; i < 4; i++){
        int ny = y + dy[i];
        int nx = x + dx[i];
        if(ny < 0 || ny >= n || nx < 0 || nx >= m || visited[ny][nx]) continue;
        visited[ny][nx] = 1;
        if(a[ny][nx] == '1'){
            v.push_back({ny, nx});
            continue;
        }
        dfs(ny, nx);
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin >> n >> m >> x1 >> y1 >> x2 >> y2;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cin >> a[i][j];
        }
    }
    
    while(true){
        fill(&visited[0][0], &visited[0][0] + 301 * 301, 0);
        v.clear();
        
        visited[y1 - 1][x1 - 1] = 1;
        dfs(y1 - 1, x1 - 1);
        
        if(isEnd) break;
        
        for(auto b : v) a[b.first][b.second] = '0';
        cnt++;
    }
    cout << cnt << '\n';
}