title: Converting a 2D matrix into a graph
body: <p>I am facing a problem while converting a given 2D matrix containing both invalid and valid points into a graph with only valid nodes. The problem goes like this.
  I have a 2D matrix like</p>
  
  <pre><code># # # # #
  # . C C #
  # S # # #
  # . . E #
  # # # # # 
  </code></pre>
  
  <p>I want to find the shortest distance from S to E keeping in mind that I have to cover all 'C' and '#' acts as a wall and '.' acts as free path. 
  Now I want to convert this matrix into a graph containing only the valid nodes. 
  Kindly help me out.</p>
  
  <pre><code>n = number of nodes
  for i=1 to n: for j=1 to n: d[i][j]=INF
   for k=1 to n:
     for i=1 to n:
      for j=1 to n:
          d[i][j] = min(d[i][j], d[i][k] + d[k][j])
  
  shortest = INF
  for each permutation a[1],a[2],...a[k] of the 'mustpass' nodes:
    shortest = min(shortest, d['start'][a[1]]+d[a[1]][a[2]]+...+d[a[k]]['end'])
  print shortest
  </code></pre>