Language: C++

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;

#define int long long

const int CN = 1e3 + 3;
const int INF = 0x3f3f3f3f3f3f3f3f;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

int read(){
    int s = 0,ne = 1; char c = getchar();
    while(c < '0' || c > '9') ne = c == '-' ? -1 : 1, c = getchar();
    while(c >= '0' && c <= '9') s = (s << 1) + (s << 3) + c - '0', c = getchar();
    return s * ne;
}

class fs {public: int to,nxt,w; void init(int t,int n,int d) {to = t, nxt = n, w = d;} } E[CN * CN * 100];
int hd[CN * CN], ecnt = 0; void add(int x,int y,int z) {E[++ecnt].init(y, hd[x], z); hd[x] = ecnt;}

int n, m, col[CN][CN], idx = 0, A, B, C, D; bool can[CN][CN]; queue<int> x, y; map<int, bool> con[CN * CN];

class DJ {public: int v, id; bool operator < (const DJ &a) const {return v > a.v;}} ;
DJ mk(int a, int b) {DJ c; c.v = a, c.id = b; return c;}
bool vis[CN * CN]; int d[CN * CN]; priority_queue<DJ> Q;
void SP(int s){
    memset(d, 0x3f, sizeof(d)), Q.push( mk(d[s] = 0, 1) );
    while(!Q.empty()){
        int u = Q.top().id; Q.pop(); if(vis[u]) continue; vis[u] = true;
        for(int k = hd[u]; k; k = E[k].nxt){
            int v = E[k].to; if(vis[v]) continue;
            if(d[v] > d[u] + E[k].w) d[v] = d[u] + E[k].w, Q.push( mk(d[v], v) );
        }
    }
}

signed main()
{
    // freopen("_in.in", "r", stdin);
    n = read(), m = read(), A = read(), B = read(), C = read(), D = read();
    for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++){
        char c; cin >> c; can[i][j] = c == '#' ? 0 : 1;
    } 
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++){
            if(col[i][j] || !can[i][j]) continue; idx++;
            x.push(i), y.push(j);
            while(!x.empty()){
                int a = x.front(), b = y.front(); x.pop(), y.pop();
                col[a][b] = idx;
                for(int k = 0; k < 4; k++){
                    int vx = a + dx[k], vy = b + dy[k];
                    if(!vx || !vy || vx > n || vy > m || col[vx][vy] || !can[vx][vy]) continue;
                    x.push(vx), y.push(vy);
                }
            }
        }
    for(int i = 1; i <= n; i++)
    for(int j = 1; j <= m; j++)
    for(int k = max(1ll, i - 3); k <= min(n, i + 3); k++)
    for(int p = max(1ll, j - 3); p <= min(m, j + 3); p++){
        int u = col[i][j], v = col[k][p]; if(con[u].count(v) || u == v) continue; con[u][v] = con[v][u] = 1;         
        add(col[k][p], col[i][j], 1), add(col[i][j], col[k][p], 1);
    }
    SP(col[A][B]), printf("%lld", d[col[C][D]] < INF ? d[col[C][D]] : -1);
}

Compiler output:
/tmp/ccmukUaI.o: in function `add(long long, long long, long long)':
a.cc:(.text+0xa9): relocation truncated to fit: R_X86_64_PC32 against symbol `ecnt' defined in .bss section in /tmp/ccmukUaI.o
a.cc:(.text+0xb4): relocation truncated to fit: R_X86_64_PC32 against symbol `ecnt' defined in .bss section in /tmp/ccmukUaI.o
a.cc:(.text+0xbb): relocation truncated to fit: R_X86_64_PC32 against symbol `ecnt' defined in .bss section in /tmp/ccmukUaI.o
a.cc:(.text+0xe6): relocation truncated to fit: R_X86_64_PC32 against symbol `hd' defined in .bss section in /tmp/ccmukUaI.o
a.cc:(.text+0x101): relocation truncated to fit: R_X86_64_PC32 against symbol `ecnt' defined in .bss section in /tmp/ccmukUaI.o
a.cc:(.text+0x114): relocation truncated to fit: R_X86_64_PC32 against symbol `hd' defined in .bss section in /tmp/ccmukUaI.o
/tmp/ccmukUaI.o: in function `SP(long long)':
a.cc:(.text+0x15e): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/ccmukUaI.o
a.cc:(.text+0x179): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/ccmukUaI.o
a.cc:(.text+0x194): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/ccmukUaI.o
a.cc:(.text+0x1bb): relocation truncated to fit: R_X86_64_PC32 against symbol `Q' defined in .bss section in /tmp/ccmukUaI.o
a.cc:(.text+0x1cf): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status