Expand The Pond(Approach similar to editorial but still not passting 3 test cases)

#include <bits/stdc++.h>

using namespace std;
#define int long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define deb(x) cout << #x << “=” << x << endl
#define deb2(x, y) cout << #x << “=” << x << “,” << #y << “=” << y << endl
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, k, n) for (int i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
typedef vector vi;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int>> pipii;
typedef priority_queue<int, vector, greater> minpq;
typedef priority_queue maxpq;
//const double PI = acos(-1);
const int mod = 1000000007;

// ----------------------DEBUG TEMPLATE--------------------------
void __print(int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << ‘’’ << x << ‘’’; }
void __print(const char *x) { cerr << ‘"’ << x << ‘"’; }
void __print(const string &x) { cerr << ‘"’ << x << ‘"’; }
void __print(bool x) { cerr << (x ? “true” : “false”); }

template <typename T, typename V>
void __print(const pair<T, V> &x)
{
cerr << ‘{’;
__print(x.first);
cerr << ‘,’;
__print(x.second);
cerr << ‘}’;
}
template
void __print(const T &x)
{
int f = 0;
cerr << ‘{’;
for (auto &i : x)
cerr << (f++ ? “,” : “”), __print(i);
cerr << “}”;
}
void _print() { cerr << “]\n”; }
template <typename T, typename… V>
void _print(T t, V… v)
{
__print(t);
if (sizeof…(v))
cerr << ", ";
_print(v…);
}
#ifndef ONLINE_JUDGE
#define debug(x…)
cerr << “[” << #x << “] = [”;
_print(x)
#else
#define debug(x…)
#endif
// ----------------------DEBUG TEMPLATE--------------------------
#define N 1001

int mat[N][N];
bool vis[N][N] = {false};
int R, C;
int dr[] = {1, -1, 0, 0};
int dc[] = {0, 0, -1, 1};
int id = 1;
map<int, int> freq;
void bfs(int r, int c, int id)
{

vis[r][c] = true;
mat[r][c] = id;
// freq[id]++;
queue<int> qr;
queue<int> qc;

qr.push(r);
qc.push(c);

while (!qr.empty())
{
    int tr = qr.front();
    qr.pop();
    int tc = qc.front();
    qc.pop();

    mat[tr][tc] = id;
    freq[id]++;
    fo(i, 4)
    {
        int rr = tr + dr[i];
        int cc = tc + dc[i];

        if (rr < 0 || cc < 0 || rr >= R || cc >= C || vis[rr][cc] == true )
        {
            continue;
        }
        if (vis[rr][cc] == false && mat[rr][cc] == 1)
        {
            vis[rr][cc] = true;
            qr.push(rr);
            qc.push(cc);
        }
    }
}

}

void giveId()
{
fo(i, R)
{
fo(j, C)
{
if (vis[i][j] == false && mat[i][j] == 1)
{
id++;
bfs(i, j, id);
}
}
}
}

int32_t main()
{

cin >> R >> C;
fo(i, R)
{
    fo(j, C)
    {
        cin >> mat[i][j];
    }
}
giveId();

int ans = 0;
unordered_set<int> s;
int checkvis[R][C]={false}; 
fo(i, R)
{
    fo(j,C){
        if (mat[i][j] == 0)
        {
            fo(k, 4)
            {
                int rr = i + dr[k];
                int cc = j + dc[k];
                if (rr < 0 || cc < 0 || rr >= R || cc >= C || mat[rr][cc]==0)
                {
                    continue;
                }
                s.insert(mat[rr][cc]);
            }
            int tempAns=0;
            tr(it,s){
                tempAns += freq[(*it)];
            }
            ans = max(ans,tempAns+1);
            s.clear();
        }
    }
}
cout<<ans<<endl;

}