Submission

Status:

[PPPPPPPPPPPPPPPPPPPPPPPPP]

Subtask/Task Score:

{100/100}

Score: 100

User: Bune

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

Language: cpp

Time: 0.003 second

Submitted On: 2026-02-28 20:40:02

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

#define fastio ios_base::sync_with_stdio(false); cin.tie(nullptr);
#define ll long long
#define pb push_back
#define all(x) (x).begin(), (x).end()

int main() {
  fastio

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

  vector<string> v;

  for (int i = 0; i < N; i++) {
    string a;
    cin >> a;
    v.pb(a);
  }

  queue<pair<int, int>> q;
  vector<int> dx = {-1, 0, 0, 1}, dy = {0, -1, 1, 0};
  vector<vector<bool>> vis(N, vector<bool>(M, false));

  int ans = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (v[i][j] == '1' && !vis[i][j]) {
        q.push({i, j});
        vis[i][j] = true;

        int size = 0;

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

          for (int k = 0; k < 4; k++) {
            int xx = x + dx[k];
            int yy = y + dy[k];
            if (xx < 0 || yy < 0 || xx > N - 1 || y > M - 1) continue;
            if (v[xx][yy] == '1' && !vis[xx][yy]) {
              q.push({xx, yy});
              vis[xx][yy] = true;
            }
          }
        }

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

  cout << ans << "\n";

  return 0;
}