Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<pair<int, int>> v;
char ch[1001][1001];
bool vis1[1001][1001];
void dfs(int c1, int c2, int n, int m) {
  if (vis1[c1][c2] == 1)
    return;
  vis1[c1][c2] = 1;
  v.push_back({c1, c2});
  if ((c1 + 1) <= n && ch[c1 + 1][c2] == '.')
    dfs(c1 + 1, c2, n, m);
  if ((c1 - 1) > 0 && ch[c1 - 1][c2] == '.')
    dfs(c1 - 1, c2, n, m);
  if (c2 + 1 <= m && ch[c1][c2 + 1] == '.')
    dfs(c1, c2 + 1, n, m);
  if (c2 - 1 > 0 && ch[c1][c2 - 1] == '.')
    dfs(c1, c2 - 1, n, m);
}
int32_t main() {
  int t, n, m, i, j, k = 1e9 + 7, l = 0, x = -1, l1, l2, c1, c2, d1, d2;
  cin >> n >> m;
  cin >> c1 >> c2;
  cin >> d1 >> d2;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      cin >> ch[i][j];
  queue<pair<int, pair<int, int>>> q;
  q.push({c1, {c2, 0}});
  bool vis[n + 1][m + 1];
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      vis[i][j] = 0;
  memset(vis1, 0, sizeof(vis1));
  while (!q.empty()) {
    auto it = q.front();
    j = it.first;
    k = it.second.first;
    l = it.second.second;
    q.pop();
    if (vis[j][k] == 1)
      continue;
    vis[j][k] = 1;
    dfs(j, k, n, m);
    for (i = 0; i < v.size(); i++) {
      l1 = v[i].first, l2 = v[i].second;
      vis[l1][l2] = 1;
      if (l1 == d1 && l2 == d2) {
        x = l;
        break;
      }
    }
    if (x != -1)
      break;
    for (i = 0; i < v.size(); i++) {
      l1 = v[i].first, l2 = v[i].second;
      for (j = l1 - 2; j <= l1 + 2; j++) {
        for (k = l2 - 2; k <= l2 + 2; k++) {
          if (j >= 1 && j <= n && k >= 1 && k <= m) {
            if (ch[j][k] == '.' && vis[j][k] == 0) {
              if (j == d1 && k == d2) {
                x = l + 1;
                break;
              }
              q.push({j, {k, l + 1}});
            }
          }
          if (x != -1)
            break;
        }
        if (x != -1)
          break;
      }
      if (x != -1)
        break;
    }
    if (x != -1)
      break;
    v.clear();
  }
  cout << x;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<pair<int, int>> v;
char ch[1001][1001];
bool vis1[1001][1001];
void dfs(int c1, int c2, int n, int m) {
  if (vis1[c1][c2] == 1)
    return;
  vis1[c1][c2] = 1;
  v.push_back({c1, c2});
  if ((c1 + 1) <= n && ch[c1 + 1][c2] == '.')
    dfs(c1 + 1, c2, n, m);
  if ((c1 - 1) > 0 && ch[c1 - 1][c2] == '.')
    dfs(c1 - 1, c2, n, m);
  if (c2 + 1 <= m && ch[c1][c2 + 1] == '.')
    dfs(c1, c2 + 1, n, m);
  if (c2 - 1 > 0 && ch[c1][c2 - 1] == '.')
    dfs(c1, c2 - 1, n, m);
}
int32_t main() {
  int t, n, m, i, j, k = 1e9 + 7, l = 0, x = -1, l1, l2, c1, c2, d1, d2;
  cin >> n >> m;
  cin >> c1 >> c2;
  cin >> d1 >> d2;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      cin >> ch[i][j];
  queue<pair<int, pair<int, int>>> q;
  q.push({c1, {c2, 0}});
  bool vis[n + 1][m + 1];
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      vis[i][j] = 0;
  while (!q.empty()) {
    auto it = q.front();
    j = it.first;
    k = it.second.first;
    l = it.second.second;
    q.pop();
    if (vis[j][k] == 1)
      continue;
    vis[j][k] = 1;
    memset(vis1, 0, sizeof(vis1));
    dfs(j, k, n, m);
    for (i = 0; i < v.size(); i++) {
      l1 = v[i].first, l2 = v[i].second;
      vis[l1][l2] = 1;
      if (l1 == d1 && l2 == d2) {
        x = l;
        break;
      }
    }
    if (x != -1)
      break;
    for (i = 0; i < v.size(); i++) {
      l1 = v[i].first, l2 = v[i].second;
      for (j = l1 - 2; j <= l1 + 2; j++) {
        for (k = l2 - 2; k <= l2 + 2; k++) {
          if (j >= 1 && j <= n && k >= 1 && k <= m) {
            if (ch[j][k] == '.' && vis[j][k] == 0) {
              if (j == d1 && k == d2) {
                x = l + 1;
                break;
              }
              q.push({j, {k, l + 1}});
            }
          }
          if (x != -1)
            break;
        }
        if (x != -1)
          break;
      }
      if (x != -1)
        break;
    }
    if (x != -1)
      break;
    v.clear();
  }
  cout << x;
}