考え方
止まるマスと訪れたマスを別に管理してBFSをする.
回答例
from collections import deque
N, M = map(int, input().split())
S = [input() for _ in range(N)]
seen = [[False]*M for _ in range(N)]
stop = [[False]*M for _ in range(N)]
delta = [(-1, 0), (1, 0), (0, -1), (0, 1)]
q = deque()
q.append((1, 1))
seen[1][1] = True
stop[1][1] = True
while q:
cx, cy = q.popleft()
for dx, dy in delta:
nx, ny = cx, cy
while S[nx + dx][ny + dy] != '#':
nx, ny = nx + dx, ny + dy
seen[nx][ny] = True
if stop[nx][ny]:continue
stop[nx][ny] = True
q.append((nx, ny))
print(sum(seen[i][j] for i in range(N) for j in range(M)))