Submission

Status:
[PPPPPPPPPPPPPPPPPPPPPPPPP]

Score: 100

User: Cmoss9

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

Language: cpp

Time: 0.004 second

Submitted On: 2025-03-28 09:17:04

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

int dfs(vector<vector<int>>& field, int row, int col, int n, int m) {
    if (row >= n || col >= m || row < 0 || col < 0) {
        return 0;
    }
    if (field[row][col] == 0) {
        return 0;
    }
    field[row][col] = 0;
    int count = 1;
    count += dfs(field,row-1,col,n,m);
    count += dfs(field,row+1,col,n,m);
    count += dfs(field,row,col-1,n,m);
    count += dfs(field,row,col+1,n,m);
    return count;

}

int main () {
    cin.tie(0) -> sync_with_stdio(0);
    int n,m;
    cin >> n >> m;
    vector<vector<int>> field(n,vector<int>(m));
    for (int i = 0;i<n;i++) {
        string temp;
        cin >> temp;
        for (int j = 0;j<m;j++) {
            field[i][j] = temp[j] - '0';
        }
    }
    int count = 0;
    for (int i = 0;i<n;i++) {
        for (int j = 0;j<m;j++) {
            if (field[i][j] == 1) {
                count = max(count,dfs(field,i,j,n,m));
            }
        }
    }
    cout << count;
}