Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
We consider each cell of <code>grid</code> a node on a graph, and there is an edge between two nodes if one of them doesn't contain <code>-1</code> and they share an edge on the grid.
We find the connected components of said graph.
The nodes of each connected component have the same value for <code>R</code> and it's the sum of the values of all nodes in other connected components.