title: Determine if all the elements of a matrix are connected in Python
body: <p><strong>Note: Do not mention Numpy for matrix creation, as I cannot use that specific library</strong>.</p>
  
  <p>I've been working on a Python program that checks if all the elements inside a board (the board size can change) are connected. For example, this board's elements are all connected:</p>
  
  <pre><code>board = [
      [1, 0, 0, 1],
      [0, 1, 1, 0],
      [0, 0, 1, 0],
      [0, 1, 0, 0],
  ]
  </code></pre>
  
  <p>However, the current program I have is faulty, as some specific cases return the opposite Boolean value. What should I be doing instead?</p>
  
  <p>This is the current code I have:</p>
  
  <pre><code>#Should return False, returns False
  board1 = [
      [1, 0, 1],
      [1, 0, 1],
      [0, 0, 1]
      ]
  
  #Should return True, returns False
  board2 = [
      [1, 0, 1],
      [1, 0, 1],
      [0, 1, 0]
      ]
  
  #Should return True, returns True
  board3 = [
      [0, 1, 0],
      [1, 1, 1],
      [0, 1, 0]
      ]
  
  #Should return True, returns False
  board4 = [
      [0, 0, 0, 1],
      [0, 1, 1, 0],
      [0, 0, 1, 0],
      [0, 1, 0, 0],
      ]
  def check(board):
      adjacent_total = []
  
      for x in range(len(board)):
          for y in range(len(board[0])):
              adjacent = []
  
              if board[x][y] == 1:
  
                  for i in range (x-1, x+2):
                      for j in range (y-1, y+2):
                          if i == x and j == y:
                              continue
                          if i == len(board) or j == len(board[0]):
                              break
  
                          if i &gt;= 0 and j &gt;= 0:
                              if board[i][j] == 1:
                                  adjacent.append((i,j))
  
              else:
                  adjacent = None
              adjacent_total.append(adjacent)
  
      for i in adjacent_total:
  
          if i is None:
              continue
          elif len(i) == 1:
              return False
      return True
  
  print(check(board1))
  print(check(board2))
  print(check(board3))
  print(check(board4))
  </code></pre>