h,w=map(int,input().split())
l=[]
for i in range(h):
    l.append(list(input()))
p=[[0 for i in range(w)] for j in range(h)]
#q=[[0 for i in range(w)] for j in range(h)]
for i in range(h):
    f=0
    for j in range(w-1,-1,-1):
        if l[i][j]=='O':
            f+=1
        elif l[i][j]=='J':
            p[i][j]=f
a=0
for i in range(w):
    f=0
    for j in range(h-1,-1,-1):
        if l[j][i]=='I':
            f+=1
        elif l[j][i]=='J':
            a+=p[j][i]*f
print(a)
