Submission

Status:

[PPPPPPPPPPPPPPPPPPPPPPPPP]

Subtask/Task Score:

{100/100}

Score: 100

User: Overlord12345

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

Language: cpp

Time: 0.005 second

Submitted On: 2025-12-07 13:15:58

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;

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

    vector<vector<bool>> visited(N, vector<bool>(M, false));
    int ans = 0;

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

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (grid[i][j] == '1' && !visited[i][j]) {

                queue<pair<int,int>> q;
                q.push({i, j});
                visited[i][j] = true;
                int count = 0;

                while (!q.empty()) {
                    auto [x, y] = q.front();
                    q.pop();
                    count++;

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

                        if (nx >= 0 && nx < N && ny >= 0 && ny < M) {
                            if (!visited[nx][ny] && grid[nx][ny] == '1') {
                                visited[nx][ny] = true;
                                q.push({nx, ny});
                            }
                        }
                    }
                }

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

    cout << ans;
    return 0;
}