Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- m, n = map(int, input().split())
- matrix = []
- while m:
- matrix.append(list(input()))
- m -= 1
- count = 0
- for i in range(len(matrix)):
- for j in range(len(matrix[0])):
- if matrix[i][j] == '#':
- count += 1
- matrix[i][j] = '.'
- st = [(i, j)]
- while st:
- x, y = st.pop()
- for xp, yp in [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]:
- if xp >= 0 and yp >= 0 and xp < len(matrix) and yp < len(matrix[0]):
- if matrix[xp][yp] == '#':
- st.append((xp, yp))
- matrix[xp][yp] = '.'
- print(count)
Advertisement
Add Comment
Please, Sign In to add comment