title: Scala: fold over two-dimensional array
body: <p>Given a potentially large image (or two-dimensional array of numbers), I'd like to loop over all the pixels (or numbers) and e.g. count all the black ones (or 0 values).</p>
  
  <p>I know I can simply use a for comprehension like</p>
  
  <pre><code>for (y &lt;- 0 until img.height; x &lt;- 0 until img.width) {
      ...
  }
  </code></pre>
  
  <p>but then I need a mutable variable for the count.  Which may not really be a problem, but suppose I don't want this and would like to use a more functional style, how do I do this without creating a large data structure with width x height elements (in other words keeping the memory-efficiency of the <code>0 until img.height</code> and <code>0 until img.width</code> ranges)?</p>