serega1112

Count squares

Mar 7th, 2021
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1.  
  2. m, n = map(int, input().split())
  3.  
  4. matrix = []
  5. while m:
  6.     matrix.append(list(input()))
  7.     m -= 1
  8.  
  9. count = 0
  10.  
  11. for i in range(len(matrix)):
  12.     for j in range(len(matrix[0])):
  13.         if matrix[i][j] == '#':
  14.             count += 1
  15.             matrix[i][j] = '.'
  16.  
  17.             st = [(i, j)]
  18.             while st:
  19.                 x, y = st.pop()
  20.                 for xp, yp in [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]:
  21.                     if xp >= 0 and yp >= 0 and xp < len(matrix) and yp < len(matrix[0]):
  22.                         if matrix[xp][yp] == '#':
  23.                             st.append((xp, yp))
  24.                             matrix[xp][yp] = '.'
  25.  
  26. print(count)
Advertisement
Add Comment
Please, Sign In to add comment