5
public Point(int x, int y) {13
int[] dx = {0, 0, +1, -1};14
int[] dy = {+1, -1, 0, 0};16
public int maxDistance(int[][] grid) {19
Queue<Point> q = new LinkedList<>();20
for (int i = 0; i < n; i++) {21
for (int j = 0; j < n; j++) {22
if (grid[i][j] == 0) dist[i][j] = 2 * 1000 * 1000 * 1000;25
q.add(new Point(i, j)); // multisource on grid[i][j] = 130
while (!q.isEmpty()) {34
for (int k = 0; k < 4; k++) {37
if (valid(r, c) && grid[r][c] == 0 && dist[r][c] > dist[x][y] + 1) {38
dist[r][c] = dist[x][y] + 1;39
Point newP = new Point(r, c);44
for (int i = 0; i < n; i++) {45
for (int j = 0; j < n; j++) {50
j]); // Manhattan distance is the same as the distance on the grid in general53
if (ans == 0 || ans == 2 * 1000 * 1000 * 1000) ans = -1;57
public boolean valid(int r, int c) {58
return r >= 0 && c >= 0 && r < n && c < n;