human
Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix | Function to find the maximum distance.
Set up top row and left column.
Pass one : top left to bottom right.
Check if there was no " One " Cell.
Set up top row and left column.
Past two : bottom right to top left.
Driver code"
How can the above be solved in Python?
 Give me a code to maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix and the other steps required?
assistant
#Here is code to maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix and the other steps required.
 def maxDistance ( grid ) :
	if ( len ( grid ) == 0 ) :
		return - 1
	N = len ( grid )
	INF = 1000000
	if grid [ 0 ] [ 0 ] == 1 :
		grid [ 0 ] [ 0 ] = 0
	else :
		grid [ 0 ] [ 0 ] = INF
	for i in range ( 1 , N ) :
		if grid [ 0 ] [ i ] == 1 :
			grid [ 0 ] [ i ] = 0
		else :
			grid [ 0 ] [ i ] = grid [ 0 ] [ i - 1 ] + 1
	for i in range ( 1 , N ) :
		if grid [ i ] [ 0 ] == 1 :
			grid [ i ] [ 0 ] = 0
		else :
			grid [ i ] [ 0 ] = grid [ i - 1 ] [ 0 ] + 1
	for i in range ( 1 , N ) :
		for j in range ( 1 , N ) :
			if grid [ i ] [ j ] == 1 :
				grid [ i ] [ j ] = 0
			else :
				grid [ i ] [ j ] = min ( grid [ i - 1 ] [ j ] , grid [ i ] [ j - 1 ] + 1 )
	if ( grid [ N - 1 ] [ N - 1 ] >= INF ) :
		return - 1
	maxi = grid [ N - 1 ] [ N - 1 ]
	for i in range ( N - 2 , - 1 , - 1 ) :
		grid [ N - 1 ] [ i ] = min ( grid [ N - 1 ] [ i ] , grid [ N - 1 ] [ i + 1 ] + 1 )
		maxi = max ( grid [ N - 1 ] [ i ] , maxi )
	for i in range ( N - 2 , - 1 , - 1 ) :
		grid [ i ] [ N - 1 ] = min ( grid [ i ] [ N - 1 ] , grid [ i + 1 ] [ N - 1 ] + 1 )
		maxi = max ( grid [ i ] [ N - 1 ] , maxi + 1 )
	for i in range ( N - 2 , - 1 , - 1 ) :
		for j in range ( N - 2 , - 1 , - 1 ) :
			grid [ i ] [ j ] = min ( grid [ i ] [ j ] , min ( grid [ i + 1 ] [ j ] + 1 , grid [ i ] [ j + 1 ] + 1 ) )
			maxi = max ( grid [ i ] [ j ] , maxi )
	if maxi == 0 :
		return - 1
	else :
		return maxi
arr = [ [ 0 , 0 , 1 ] , [ 0 , 0 , 0 ] , [ 0 , 0 , 0 ] ]
print ( maxDistance ( arr ) )