Submission

Status:

[PPPPPPPPPPPPPPPPPPPPPPPPP]

Subtask/Task Score:

{100/100}

Score: 100

User: Chawin

Problemset: เกาะที่ใหญ่ที่สุด

Language: cpp

Time: 0.003 second

Submitted On: 2026-02-22 14:14:29

#include<bits/stdc++.h>
using namespace std;

int main(){
    ios_base::sync_with_stdio(0), cin.tie(0);

    int n, m;
    cin >> n >> m;

    vector<string> mp(n);
    for(int i = 0; i < n; i++){
        cin >> mp[i];
    }

    vector<vector<bool>> visited(n, vector<bool>(m, false));

    int dx[4] = {1, -1, 0, 0};
    int dy[4] = {0, 0, 1, -1};

    int ans = 0;
    queue<pair<int, int>> q;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(mp[i][j] == '1' && !visited[i][j]){
                q.push({i, j});
                visited[i][j] = true;

                int cnt = 1;

                while(!q.empty()){
                    int x = q.front().first;
                    int y = q.front().second;
                    q.pop();

                    for(int d = 0; d < 4; d++){
                        int nx = x + dx[d];
                        int ny = y + dy[d];

                        if(nx >= 0 && nx < n && ny >= 0 && ny < m){
                            if(mp[nx][ny] == '1' && !visited[nx][ny]){
                                q.push({nx, ny});
                                visited[nx][ny] = true;
                                cnt++;
                            }
                        }
                    }
                }

                ans = max(ans, cnt);
            }
        }
    }

    cout << ans;

    return 0;
}