# Farthest Distance From Lands

Posted: 30 Mar, 2021

Difficulty: Moderate

#### You are given a binary square matrix ‘ARR’ with N rows and N columns, in which 0 represents the water and 1 represents the land.

#### You have to find a water cell such that its distance to the nearest land cell is maximized and print the maximum distance.

#### Note :

```
The distance between any two cells (x0, y0) and (x1, y1) is given by the Manhattan distance: |x0 - x1| + |y0 - y1|.
If no land or water exists in the grid, then return -1.
```

##### Input Format :

```
The first line of input contains an integer ‘T' representing the number of test cases.
The first line of each test case contains one integer ‘N’ denoting the size of the matrix.
The next ‘N’ lines contain ‘N’ integers separated by spaces describing rows of matrix ‘ARR’ (each element of ‘ARR’ is either 0 or 1).
```

##### Output Format :

```
For each test case, on a separate line, output one integer - the largest distance from a water cell to the nearest land cell.
```

##### Note :

```
You do not need to print anything. It has already been taken care of. Just implement the given function.
```

##### Constraints :

```
1 <= T <= 5
1 <= N <= 10^3
ARR[i][j] = 0 or 1
Time limit: 1 sec
```

SIMILAR PROBLEMS

Is Graph Bipartite?

Posted: 28 Jan, 2022

Difficulty: Moderate

Valid Arrangement of Pairs

Posted: 28 Jan, 2022

Difficulty: Hard

Valid Arrangement of Pairs

Posted: 28 Jan, 2022

Difficulty: Hard

Left Right Print

Posted: 9 Jul, 2022

Difficulty: Moderate

COUNT ISLANDS

Posted: 14 Sep, 2022

Difficulty: Moderate

The Summit

Posted: 15 Sep, 2022

Difficulty: Easy