#include <bits/stdc++.h>
using namespace std;
int ocnt[3001][3001] = {0}, kcnt[3001][3001] = {0};
char s[3000][3001];
int main(void) {
  int h, w, sum, i, j;
  scanf("%d %d", &h, &w);
  for (i = 0; i < h; i++) {
    scanf("%s", s[i]);
  }
  for (i = 0; i < h; i++) {
    for (j = w - 1; j > 0; j--) {
      if (s[i][j] == 'O') {
        ocnt[i][j - 1]++;
        ocnt[i][j] += ocnt[i][j + 1];
      }
    }
  }
  for (i = 0; i < w; i++) {
    for (j = h - 1; j >= 0; j--) {
      if (s[j][i] == 'I') {
        s[j - 1][i]++;
        s[j][i] += s[j + 1][i];
      }
    }
  }
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      printf("%d", ocnt[i][j]);
    }
    printf("\n");
  }
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      printf("%d", kcnt[i][j]);
    }
    printf("\n");
  }
  sum = 0;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (s[i][j] == 'J') {
        sum += kcnt[i][j] * ocnt[i][j];
      }
    }
  }
  printf("%d\n", sum);
  return 0;
}
