# 입력
import sys
from collections import deque
n,m = map(int,sys.stdin.readline().split())
maps = [list(sys.stdin.readline().strip()) for _ in range(n)]
visited = [[False] * m for _ in range(n)]
dx = [0,0,1,-1]
dy = [1,-1,0,0]
# 처리
def bfs(where):
count = 0
i,j = where[0],where[1]
queue = deque()
visited[i][j] = True
queue.append((i,j))
while queue:
i, j = queue.popleft()
for k in range(4):
x = i + dx[k]
y = j + dy[k]
if 0<=x<n and 0<=y<m and not visited[x][y]:
if maps[x][y] != 'X':
queue.append((x,y))
visited[x][y] = True
if maps[x][y] == 'P':
count += 1
return count
def get_index(maps, target):
for idx1, i in enumerate(maps):
for idx2, j in enumerate(i):
if j == target:
return idx1, idx2
ans = bfs(get_index(maps, 'I'))
print('TT' if ans == 0 else ans)