title: Find clusters of bacteria
body: <p>I have to find all clusters of bacteria that are connected (4-connectivity) in a Python program. The input is a file that looks like this:</p>
  
  <pre><code>                     ###
                       #####
                       #######
                      #######
                       ######
                       ###### ##
                       ####  #####
                         ## ######        ####
                      #    ######       ####
                    ###  ##########    #####
                  #######  ####  ##   ######
                  ######### ##   #      #####
        #           ####   ###          ###
       #####        ####    #     ##     ##
       #####                    ######    #
      ######                   ########
       ####                     ########
                                #######
                                #######
  </code></pre>
  
  <p>NOTE: Clusters that are adjacent to the edge of the grid cannot be counted</p>
  
  <p>This file is saved in the form of a 2D array in my class. I wrote this function to find all the clusters but it creates to many clusters (22 instead of 5). Any idea what I might be doing wrong?</p>
  
  <p>My code:</p>
  
  <pre><code>def findAll(self):
      self.colonies = [set()]
      for i in range(len(self.grid)):
          for j in range(len(self.grid[i])):
              if self.grid[i][j] == "#":
                  added = False
                  count = 0
                  for k in self.colonies:
                      if self.checkNeighbours((i, j), k):
                          k.add((i, j))
                          added = True
                      count += 1
                  if not added:
                      self.colonies.append({(i, j)})
  
  def checkNeighbours(self, pos, current):
      return ((pos[0] + 1, pos[1]) in current
              or (pos[0] - 1, pos[1]) in current
              or (pos[0], pos[1] + 1) in current
              or (pos[0], pos[1] - 1) in current)
  </code></pre>