Language: C++

#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define ld long double
 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
 
template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
string to_string(char c) {
	return string(1, c);
}
string to_string(bool b) {
	return b ? "true" : "false";
}
string to_string(const char* s) {
	return string(s);
}
string to_string(string s) {
	return s;
}
string to_string(vector<bool> v) {
	string res;
	for(int i = 0; i < (int) v.size(); i++)
		res += char('0' + v[i]);
	return res;
}
 
template<size_t S> string to_string(bitset<S> b) {
	string res;
	for(int i = 0; i < S; i++)
		res += char('0' + b[i]);
	return res;
}

template<class T> string to_string(T v) {
    bool f = 1;
    string res;
    for(auto& x : v) {
		if(!f)
			res += ' ';
		f = 0;
		res += to_string(x);
	}
    return res;
}
 
void DBG() {
	cerr << "]" << endl;
}

template<class H, class... T> void DBG(H h, T... t) {
	cerr << to_string(h);
	if(sizeof...(t))
		cerr << ", ";
	DBG(t...);
}

#ifdef _DEBUG
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif

const int d4i[4] = {-1, 0, 1, 0}, d4j[4] = {0, 1, 0, -1};
const int d8i[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8] = {0, 1, 1, 1, 0, -1, -1, -1};


vector<vector<int>> dp;
vector<vector<int>> vis;
vector<string> s;
int h, w;

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

void dfs(int x, int y){
	for(int i = 0; i < 4; i++){
		int x1 = x + d4i[i], y1 = y + d4j[i];
		if(in(x1, y1) && dp[x1][y1] != dp[x][y] && s[x1][y1] == '.'){
			dp[x1][y1] = dp[x][y];
			vis[x1][y1] = 1;
			dfs(x1, y1);
		}
		else if(in(x1, y1) && s[x1][y1] == '#'){
			for(int j = 0; j < 8; j++){
				int x2 = x1 + d8i[j], y2 = y1 + d8j[j];
				if(in(x2, y2) && (dp[x2][y2] == -1 || dp[x2][y2] > dp[x][y] + 1) && s[x2][y2] == '.'){
					dp[x2][y2] = dp[x][y] + 1;
				}
			}
		}
	}
}

void solve() {
	int ch, cw, dh, dw;
	cin >> h >> w >> ch >> cw >> dh >> dw;
	ch--; cw--; dh--; dw--;
	dp.resize(h, vector<int> (w, -1));	
	s.resize(h);
	for(int i = 1; i <= h; i++) cin >> s[i - 1];
	int m = 0;
	dp[ch][cw] = 0;
	while(true){
		bool found = 0;
		vis.resize(h, vector<int> (w, 0));
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(dp[i][j] == m && vis[i][j] == 0){
					vis[i][j] = 1;
					found = 1;
					dfs(i, j);
				}
			}
		}
		if(dp[i][j] != -1) break;
		/*
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cout << dp[i][j] << ' ';
			}
			cout << endl;
		}
		*/
		//dbg(m, dp);
		if(!found) break;
		m++;
	}
	cout << dp[dh][dw];
}	
 
int main() {
	
	ios::sync_with_stdio(0);
	cin.tie(0);
 	
	int t = 1;
	//cin >> t;
	for(int i = 1; i <= t; i++) {
		//cout << "Case #" << i << ": ";
		solve();
	}
}

Compiler output:
a.cc: In function 'void solve()':
a.cc:121:23: error: 'i' was not declared in this scope
  121 |                 if(dp[i][j] != -1) break;
      |                       ^
a.cc:121:26: error: 'j' was not declared in this scope
  121 |                 if(dp[i][j] != -1) break;
      |                          ^