
You are given an m x n grid where each cell is one of: -1 for a wall (impassable), 0 for a dashmart (a store), and INF = 2^31 - 1 for an empty room. Movement is allowed 4-directionally with cost 1 per step. You are also given an array queries of k coordinates (r, c), each guaranteed to be an empty room. For each query, return the length of the shortest path to any dashmart; return -1 if no dashmart is reachable or if the grid contains no dashmarts. Implement a function nearestDistances(grid, queries) that returns an array of k integers in the order of queries. Discuss and implement an approach that scales when k is large, and analyze its time and space complexity. Include how you would handle edge cases such as: duplicate queries, rooms completely blocked by walls, empty list of queries, or grids with zero dashmarts.