private void bfs(int[][] grid, int[][] dist, int[][] nums, int id) {
        int m = grid.length, n = grid[0].length;

        Queue<Integer> q = new LinkedList<Integer>();
        q.add(id);
        boolean[][] visited = new boolean[m][n];

        int step = 0;
        while (!q.isEmpty()) {
            final int size = q.size();
            ++step;
            for (int i = 0; i < size; ++i) {
                id = q.remove();
                int x = id / n, y = id % n;
                for (int j = 0; j < 4; ++j) {
                    int row = x + dx[j], col = y + dy[j];
                    if (row >= 0 && row < m && col >= 0 && col < n && grid[row][col] == 0 && !visited[row][col]) {
                        dist[row][col] += step;
                        ++nums[row][col];
                        visited[row][col] = true;
                        q.add(row * n + col);
                    }
                }
            }
        }
    }