#pragma comment(linker, "/STACK: 16777216")

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
#define allp(x) (x)->begin(), (x)->end()

#define pb push_back


using namespace std;
void dout() { cerr << endl; }
//typedef long long ll;
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
    cerr << H << ' ';
    dout(T...);
}

#define debug(x) cout << #x " = " << (x) << endl


using ll = long long;
//#ifdef __int128
//using hll = __int128;
//#endif
using pii = pair<ll, ll>;
using ld = long double;
using pll = pair<long long, long long>;

template <typename T>
void do_uniq(vector<T> vec){
    sort(all(vec));
    vec.resize(unique(all(vec)) - vec.begin());
}


#ifdef _getchar_nolock
#else
#define _getchar_nolock getchar_unlocked
#endif

template <typename T>
void freadT(T &x) {
    x = 0;
    char ch = _getchar_nolock();
    ll sign = 1;

    while (!isdigit(ch)) {
        if (ch == '-')
            sign *= -1;
        ch = _getchar_nolock();
    }

    while (isdigit(ch)) {
        x = x * 10 + ch - '0';
        ch = _getchar_nolock();
    }

    x *= sign;
}

int fparse(string s, int start_indx) {
    int x = 0;

    while (isdigit(s[start_indx])) {
        x = x * 10 + s[start_indx] - '0';
        ++start_indx;
    }

    return x;
}

//#define solvsh
//#define multi

#ifdef solvsh
#else


ll mod = 998244353;

ll add(ll a, ll b){
    return (a + b) % mod;
}
ll mult(ll a, ll b) {
    return (a * b) % mod;
}

const int MAXN = 1e3 + 120;

int dist[MAXN][MAXN];

char inp[MAXN][MAXN];

vector<pair<int, pair<int, int>>> g[MAXN][MAXN];

int h, w;

bool okk(int x, int y){
    return x >= 0 && y >= 0 && x < h && y < w;
}

void solve() {

    cin >> h >> w;

    int cx, cy;
    int dx, dy;

    cin >> cx >> cy >> dx >> dy;

    --cx;
    --cy;
    --dx;
    --dy;


    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            dist[i][j] = 1e9;
            cin >> inp[i][j];
        }
    }

    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            if (inp[i][j] == '#')continue;
            for (int dx = -1; dx <= 1; ++dx) {
                for (int dy = -1; dy <= 1; ++dy) {
                    if (dx * dx + dy * dy == 1) {
                        int n_x = i + dx;
                        int n_y = j + dy;

                        if (okk(n_x, n_y) && inp[n_x][n_y] != '#') {
                            g[i][j].push_back({0, {n_x, n_y}});
                        }
                    }
                }
            }


            for (int dx = -2; dx <= 2; ++dx) {
                for (int dy = -2; dy <= 2; ++dy) {
                    int n_x = i + dx;
                    int n_y = j + dy;

                    if (okk(n_x, n_y) &&  inp[n_x][n_y] != '#') {
                        g[i][j].push_back({1, {n_x, n_y}});
                    }
                }
            }
        }
    }

    dist[cx][cy] = 0;

    priority_queue<pair<int, pair<int, int>>> q;
    q.push({0, {cx, cy}});

    while (q.size()) {
        auto tp = q.top();
        q.pop();

        for (auto out : g[tp.second.first][tp.second.second]) {
            int n_w = dist[tp.second.first][tp.second.second] + out.first;
            if (n_w < dist[out.second.first][out.second.second]) {
                dist[out.second.first][out.second.second] = n_w;
                q.push({-n_w, {out.second}});
            }
        }
    }

    if (dist[dx][dy] == 1e9)
        cout << -1;
    else
        cout << dist[dx][dy];
}


#endif

clock_t timestamp_start = clock();
void time_calc()
{
    cerr << (ld)(clock() - timestamp_start) / CLOCKS_PER_SEC << "\n";
}


void multisolve() {
    int t;
    cin >> t;

    int i = 1;
    while (t--) {
        solve();
        i++;
    }
}


#define int int
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    cout << fixed << setprecision(20);

#ifndef ONLINE_JUDGE

#else

#endif
#ifdef multi
    multisolve();
#else
    solve();
//    gen();
#endif
}


Measured memory_bytes: 168108