๐งธ ๊ทธ๋์ ๋ง์ด ํ์ด๋ณธ ์ ํ์ด๋ผ ์ด๋ ต์ง ์๊ฒ ํ ์ ์์๋ค!
์ฝ๋
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const [Y, X] = input.shift().split(" ").map(Number);
const arr = input.map((item) => item.split(" ").map(Number));
const result = [];
const dir = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
];
const DFS = (i, j) => {
willCheck = [[i, j]];
arr[i][j] = -1;
let count = 0;
while (willCheck.length) {
const [pointY, pointX] = willCheck.pop();
count++;
for (let d = 0; d < 4; d++) {
const nextY = pointY + dir[d][0];
const nextX = pointX + dir[d][1];
if (
nextX >= 0 &&
nextX < X &&
nextY >= 0 &&
nextY < Y &&
arr[nextY][nextX] === 1
) {
willCheck.push([nextY, nextX]);
arr[nextY][nextX] = -1;
}
}
}
result.push(count);
};
for (let i = 0; i < Y; i++) {
for (let j = 0; j < X; j++) {
if (arr[i][j] === 1) {
DFS(i, j);
}
}
}
console.log(result.length);
console.log(result.length ? Math.max(...result) : 0);