記事の見てくれとか何も気にせず他の人の書いたコードをもとにして自分なりに書いたのをここに保存。
a = list(map(int,input().split()))
n = a[0]
m = a[1]
#print("n=",n,"m=",m)
field=[]
for i in range(n):
field.append(list(input()))
def dfs(x,y):
#print("x=",x,"y=",y)
field[x][y] = '.'
for dx in [-1,0,1]:
for dy in [-1,0,1]:
nx = x + dx
ny = y + dy
#print("nx=",nx,"ny=",ny)
if(0<=nx and nx<n and 0<=ny and ny<m and field[nx][ny] == 'W'):
dfs(nx,ny)
res = 0
for j in range(n):
for k in range(m):
print("j=",j,"k=",k,"field[",j,"][",k,"]=",field[j][k])
if(field[j][k]=='W'):
#print("so dfs starts!!")
dfs(j,k)
res +=1