8
var minDays = function (grid) {12
function colorIsland(i, j) {13
grid[i][j] = colorNumber;14
for (let k = 0; k < directions.length; k++) {15
let newI = i + directions[k][0];16
let newJ = j + directions[k][1];22
newJ < grid[0].length &&24
grid[newI][newJ] != colorNumber26
colorIsland(newI, newJ);31
function addLandCells() {32
for (let i = 0; i < grid.length; i++) {33
for (let j = 0; j < grid[0].length; j++) {35
landCells.push([i, j]);41
function findAnotherIsland() {42
for (let i = 0; i < grid.length; i++) {43
for (let j = 0; j < grid[0].length; j++) {44
if (grid[i][j] && grid[i][j] != colorNumber) {54
if (landCells.length < 2) {55
return landCells.length;58
colorIsland(landCells[0][0], landCells[0][1]);60
if (findAnotherIsland()) {66
for (let i = 0; i < landCells.length; i++) {67
grid[landCells[i][0]][landCells[i][1]] = 0;69
landCells[(i + 1) % landCells.length][0],70
landCells[(i + 1) % landCells.length][1]72
if (findAnotherIsland()) {75
grid[landCells[i][0]][landCells[i][1]] = colorNumber;