Advertisement
serega1112

Squares

Feb 18th, 2022
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.43 KB | None | 0 0
  1. m, n = map(int, input().split())
  2. g = []
  3. for i in range(m):
  4.     g.append(list(input()))
  5.  
  6.  
  7. def dfs(x, y):
  8.     g[x][y] = '.'
  9.     for xp, yp in [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]:
  10.         if 0 <= xp < m and 0 <= yp < n:
  11.             if g[xp][yp] == '#':
  12.                 dfs(xp, yp)
  13.  
  14.  
  15. count = 0
  16. for x in range(m):
  17.     for y in range(n):
  18.         if g[x][y] == '#':
  19.             count += 1
  20.             dfs(x, y)
  21.  
  22. print(count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement