2
* @param {number[][]} grid11
var maxDistance = function (grid) {12
const ROWS = grid.length;13
const COLS = grid[0].length;14
const cost = new Array(ROWS).fill().map(() => new Array(COLS).fill(Infinity));16
for (let row = 0; row < ROWS; row++) {17
for (let col = 0; col < COLS; col++) {18
if (grid[row][col] === 1) {24
if (que.length == 0 || que.length == ROWS * COLS) return -1;26
const [row, col] = que.shift();27
for (const dir of DIR) {28
const newRow = row + dir[0];29
const newCol = col + dir[1];30
if (newRow < 0 || newCol < 0 || newRow >= ROWS || newCol >= COLS)32
if (cost[newRow][newCol] > cost[row][col] + 1) {33
cost[newRow][newCol] = cost[row][col] + 1;34
que.push([newRow, newCol]);39
for (const row of cost) {40
max = Math.max(max, ...row);