title: Finding shortest distance to water
body: <p>I have a two-dimensional map of land and water, like the following (<code>l</code> representing land and <code>w</code> representing water).</p>
  
  <pre><code>lllwwwlll
  lllwllllw
  lllllllww
  lllllllll
  </code></pre>
  
  <p>For each land tile, I want to know its distance from the nearest water tile. Moving horizontally, vertically, and diagonally all count as a distance of one.</p>
  
  <pre><code>321www111
  321w1111w
  3211121ww
  322222111
  </code></pre>
  
  <p>Right now I am using an algorithm like so:</p>
  
  <pre><code>foreach tile in map
    if isWater(tile)
      tile.distanceFromWater = 0
    else
      radius = 1
      while !hasWaterAround(tile, radius)
        radius++
      tile.distanceFromWater = radius
  </code></pre>
  
  <p>This approach works, but it rather slow, especially when there are very few water tiles.</p>
  
  <p>Is there a faster algorithm for finding the distance of each land tile from water?</p>