title: How to find the largest sequence in binary array?
body: <p>I'm trying to find the largest 1's secuence inside a random array. </p>
  
  <p>I'm trying to find the longest connection of numbers 1 inside a binary array.
  I tried to compare each value of the array with its previous values (i+-1 and j+-1), but I failed.</p>
  
  <pre><code>rows = 5
  cols = 5
  
  matrix = np.random.randint(2, size=(rows, cols))
  print(matrix)
  
  count = 0
  result = 0
  for i in matrix:
      for j in i:
          if j &lt;= cols and j &lt;= len(i):
              if j == 1:
                  count += 1
                  result = max(result, count)
              else:
                  count = 0
  print(result)
  </code></pre>
  
  <p>This is an example of a random array given in my code</p>
  
  <pre><code>matrix =
  [[1 1 0 1 1]
   [1 0 0 0 1]
   [1 1 1 1 1]
   [0 0 1 0 0]
   [1 0 1 0 0]]
  </code></pre>
  
  <p>Once we have found the largest conection of numbers 1, the result will be like that:</p>
  
  <pre><code>matrix =
  [[(1) (1)  0  (1) (1)]
   [(1)  0   0   0  (1)]
   [(1) (1) (1) (1) (1)]
   [ 0   0  (1)  0   0]
   [ 1   0  (1)  0   0]]
  </code></pre>
  
  <p>The numbers inside the parentheses, those are the largest conection and the result would be like this:</p>
  
  <pre><code>result = 13
  </code></pre>
  
  <p><strong>Complete Code</strong></p>
  
  <pre><code>import numpy as np
  
  rows = 200
  cols = 200
  arr = np.random.rand(rows, cols)
  
  for i in range(len(arr)):
      for j in range(len(arr[i])):
          if arr[i][j] &gt; 0.4:
              arr[i][j] = 1
          else:
              arr[i][j] = 0
  
  for ii in range(len(arr)):
      for jj in range(len(arr[ii])):
          if arr[ii][jj] == 1.0 or arr[ii][jj] == 1:
              arr[ii][jj] = 1
          else:
              arr[ii][jj] = 0
  
  arr = arr.astype(int)
  print(arr)
  
  dimension = rows * cols
  danger = eval("dimension * .0002")
  
  
  def find_largest(arr):
      max_count = 0
      visited = set()  # coordinates visited by DFS
      for y in range(0, len(arr)):
          for x in range(0, len(arr[y])):
              if arr[y][x] == 1 and (y, x) not in visited:
                  # Runs DFS search on (y,x) and marks cell as visited
                  max_count = max(max_count, explore(arr, y, x, visited))
  
      probable_danger = suma(max_count)
      print("Quantity of connections: " + str(max_count) +
            " ,probability of danger: " + str(probable_danger) + " ,comparing to the total; " +
            str(danger))
      return max_count
  
  
  def explore(arr, y, x, visited):
      if arr[y][x] == 1 and (y, x) not in visited:
          count = 1
          visited.add((y, x))
          for i, j in adj(arr, y, x):
              count += explore(arr, i, j, visited)
          return count
      else:
          return 0
  
  
  def adj(arr, y, x):
      # Generates valid adjacent coordinates for (y, x)
      if y - 1 &gt; 0 and arr[y - 1][x] == 1:
          yield y - 1, x
      if y + 1 &lt; len(arr) and arr[y + 1][x] == 1:
          yield y + 1, x
      if x - 1 &gt; 0 and arr[y][x - 1] == 1:
          yield y, x - 1
      if x + 1 &lt; len(arr[y]) and arr[y][x + 1] == 1:
          yield y, x + 1
  
  
  def suma(max_count):
      dimension = rows * cols
      danger = eval("dimension * .0002")
      total_ones = 0
      total_zeros = 0
  
      for i in arr:
          for j in i:
              if j % 2 != 0:
                  total_ones += 1
              else:
                  total_zeros += 1
  
      found = max_count / total_ones
      ones = total_ones/dimension
      zeros = total_zeros/dimension
  
      print("Total of 1's "+str(ones) +
            " , total of 0's: "+str(zeros))
  
      if (found &gt;= danger):
          return found
      else:
          return 0
  
  
  find_largest(arr)
  
  </code></pre>