Language: C++

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

const int CN = 1e3 + 3;
const int INF = 1e9;
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];
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;
int id(int i, int j) {return (i - 1) * n + j;} map<int, int> con;

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]; int d[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) );
        }
    }
}

int 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) continue;
                    if(col[vx][vy] || !can[vx][vy]) continue;
                    x.push(vx), y.push(vy);
                }
            }
        }
    if(col[A][B] == col[C][D]) return puts("0"), 0;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            for(int k = max(1, i - 3); k <= min(n, i + 3); k++)
                for(int p = max(1, j - 3); p <= min(m, j + 3); p++){
                    int u = id(i, j), v = id(k, p);
                    if(con[u].count(v)) continue; con[u][v] = con[v][u] = true;
                    if(col[k][p] ^ col[i][j]) 
                        add(id(i, j), id(k, p), 1), add(id(k, p), id(i, j), 1);
                }
    SP(id(A, B)), printf("%d", d[id(C, D)] < INF ? d[id(C, D)] : -1);
}

Compiler output:
a.cc:25:48: error: 'map' does not name a type
   25 | int id(int i, int j) {return (i - 1) * n + j;} map<int, int> con;
      |                                                ^~~
a.cc: In function 'int main()':
a.cc:69:24: error: 'con' was not declared in this scope; did you mean 'col'?
   69 |                     if(con[u].count(v)) continue; con[u][v] = con[v][u] = true;
      |                        ^~~
      |                        col
a.cc:69:51: error: 'con' was not declared in this scope; did you mean 'col'?
   69 |                     if(con[u].count(v)) continue; con[u][v] = con[v][u] = true;
      |                                                   ^~~
      |                                                   col