Submission
Status:
[PPPP-SSSSSSSSSSSSSSSSSSSS]
Subtask/Task Score:
{0/100}
Score: 0
User: vachirasawin
Problemset: เกาะที่ใหญ่ที่สุด
Language: cpp
Time: 0.002 second
Submitted On: 2026-03-13 18:01:25
// grader-chan
// c2_st66_largest_island.cpp | c2_st66_largest_island
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, M;
cin >> N >> M;
char world[N][M] = {'0'};
bool visited[N][M] = {false};
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
// Sea 0
// Land 1
cin >> world[i][j];
}
}
int dx[] = {+1, 0, -1, 0};
int dy[] = {0, +1, 0, -1};
int ans = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (!visited[i][j] && world[i][j] == '1') {
visited[i][j] = true;
queue<pair<int, int>> q;
q.push({i, j});
int cnt = 1;
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
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) continue;
if (visited[nx][ny] || world[nx][ny] == '0') continue;
visited[nx][ny] = true;
q.push({nx, ny});
cnt++;
}
}
ans = max(ans, cnt);
}
}
}
cout << ans;
return 0;
}