public static int getMin(int[][] nums) {
int ans = 0;
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[0].length; j++) {
if (nums[i][j] == 1) {
ans++;
dfs(nums, i, j);
}
}
}
return ans;
}
public static void dfs(int[][] nums, int i, int j) {
if (i < 0 || j < 0 || i >= nums.length || j >= nums[0].length || nums[i][j] == 0) {
return;
}
nums[i][j] = 0;
dfs(nums, i - 1, j - 1);
dfs(nums, i - 1, j);
dfs(nums, i - 1, j + 1);
dfs(nums, i, j - 1);
dfs(nums, i, j + 1);
dfs(nums, i + 1, j - 1);
dfs(nums, i + 1, j);
dfs(nums, i + 1, j + 1);
}