#include <bits/stdc++.h>

int getFarthestDistance(vector<vector<int>> &grid)

{

int n=grid.size();

int m=grid[0].size();

int maxi=0;

//1st test case

queue<pair<int,pair<int,int>>>q;

vector<vector<int>>vis(n,vector<int>(m,0));

for(int i=0;i<n;i++){

for(int j=0;j<m;j++){

if(grid[i][j]==1){

q.push({0,{i,j}});

vis[i][j]=1;

}

else{

vis[i][j]=0;

}

}

}

int delrow[4] = {1,-1,0,0};

int delcol[4] = {0,0,1,-1};

while(!q.empty()){

int step = q.front().first;

int row = q.front().second.first;

int col = q.front().second.second;

q.pop();

maxi = max(maxi,step);

for(int i=0;i<4;i++){

int nrow = row+delrow[i];

int ncol = col+delcol[i];

if(nrow>=0 && nrow<n && ncol>=0 && ncol<m && !vis[nrow][ncol]

&& grid[nrow][ncol]==0){

vis[nrow][ncol]=1;

q.push({step+1,{nrow,ncol}});

}

}

}

if(maxi>0) return maxi;

return -1;

}