Language: C++

#include<bits/stdc++.h>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
ll r,c;
void go(int i,int j)
{
    q.push(make_pair(i,j));  // put them in queue
    vis[i][j]=1; // this is visited
    dis[i][j]=0; // initially no magic is needed
    while(!q.empty())
    {
        ll u=q.top().first;  // nearest point
        ll v=q.top().second;
        q.pop();
        for(int x=-2; x<=2; x++)  // try 5x5 grid
        {
            for(int y=-2; y<=2; y++)
            {
                if(u+x>r||u+x<1||v+y>c||v+y<1||(x==0&&y==0)) // out of bound
                    continue;
                if(vis[u+x][v+y]==0&&A[u+x][v+y]=='.') // possible to go
                {
                    vis[u+x][v+y]=1;
                    if(abs(x)+abs(y)>1) // if its a magic, OPeration B
                        dis[u+x][v+y]=dis[u][v]+1;
                    else  // operation A
                        dis[u+x][v+y]=dis[u][v];
                    q.push(make_pair(u+x,v+y));
                }
                else if(A[u+x][v+y]=='.') // to find minimum dis
                {
                    if((abs(x)+abs(y)>1)&&dis[u+x][v+y]>dis[u][v]+1) // can be improved
                    {                      // +1 because we are using magic
                        dis[u+x][v+y]=dis[u][v]+1;
                        q.push(make_pair(u+x,v+y));
                    }
                    else if(dis[u][v]<dis[u+x][v+y]&&(abs(x)+abs(y)<=1)) // can be improved
                    {
                        dis[u+x][v+y]=dis[u][v];
                        q.push(make_pair(u+x,v+y));
                    }
                }
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    ll sx,sy,dx,dy;
    string s;
    cin>>r>>c;
    cin>>sx>>sy;
    cin>>dx>>dy;
    for(int i=1; i<=r; i++)
    {
        cin>>s;
        for(int j=1; j<=c; j++)
        {
            A[i][j]=s[j-1];
            dis[i][j]=99999999999999; // fill dis with inf
        }
    }
    go(sx,sy); // start from source
    if(vis[dx][dy]==0) // not possible
    {
        cout<<-1<<endl;
    }
    else
    {
        cout<<dis[dx][dy]<<endl;   // minimum distance
    }
    return 0;
}

Compiler output:
a.cc: In function 'void go(int, int)':
a.cc:9:5: error: 'q' was not declared in this scope
    9 |     q.push(make_pair(i,j));  // put them in queue
      |     ^
a.cc:10:5: error: 'vis' was not declared in this scope
   10 |     vis[i][j]=1; // this is visited
      |     ^~~
a.cc:11:5: error: 'dis' was not declared in this scope; did you mean 'div'?
   11 |     dis[i][j]=0; // initially no magic is needed
      |     ^~~
      |     div
a.cc:23:38: error: 'A' was not declared in this scope
   23 |                 if(vis[u+x][v+y]==0&&A[u+x][v+y]=='.') // possible to go
      |                                      ^
a.cc: In function 'int main()':
a.cc:64:13: error: 'A' was not declared in this scope
   64 |             A[i][j]=s[j-1];
      |             ^
a.cc:65:13: error: 'dis' was not declared in this scope; did you mean 'div'?
   65 |             dis[i][j]=99999999999999; // fill dis with inf
      |             ^~~
      |             div
a.cc:69:8: error: 'vis' was not declared in this scope
   69 |     if(vis[dx][dy]==0) // not possible
      |        ^~~
a.cc:75:15: error: 'dis' was not declared in this scope; did you mean 'div'?
   75 |         cout<<dis[dx][dy]<<endl;   // minimum distance
      |               ^~~
      |               div