#include <iostream>
using namespace std;

char map[1000 * 1000];
char emb[4];

int plus_map_j[1000 * 1000] = {};
int plus_map_o[1000 * 1000] = {};
int plus_map_i[1000 * 1000] = {};

int w;
int h;

int index_map_xy(int x, int y) {
    return y * w + x;
}

int index_emb_xy(int x, int y) {
    return y * 2 + x;
}

char map_xy(int x, int y) {
    return map[index_map_xy(x, y)];
}

char emb_xy(int x, int y) {
    return emb[index_emb_xy(x, y)];
}

int process_area(int x, int y) {
    int miss_count = 0;
    int miss_plus_i;
    char miss_plus_char;
    for (int iy=0; iy<2; iy++) {
        for (int ix=0; ix<2; ix++) {
            int mi = index_map_xy(x+ix, y+iy);
            char emb = emb_xy(ix,iy);
            if (map[mi] != emb) {
                miss_count++;
                if (miss_count == 1) {
                    miss_plus_i = mi;
                    miss_plus_char = emb;
                }
            }
        }
    }
    if (miss_count == 0) {
        for (int iy=0; iy<2; iy++) {
            for (int ix=0; ix<2; ix++) {
                int i = index_map_xy(x+ix, y+iy);
                plus_map_j[i] -= 1;
                plus_map_o[i] -= 1;
                plus_map_i[i] -= 1;
            }
        }
    }
    if (miss_count == 1) {
        if (miss_plus_char == 'J') { plus_map_j[miss_plus_i] += 1; }
        if (miss_plus_char == 'O') { plus_map_o[miss_plus_i] += 1; }
        if (miss_plus_char == 'I') { plus_map_i[miss_plus_i] += 1; }
    }
    return !miss_count;
}

int main(void){
    cin >> h >> w;
    for (int i=0; i<w*h; i++) {
        cin >> map[i];
    }
    for (int i=0; i<4; i++) {
        cin >> emb[i];
    }
    
    int count = 0;
    for (int y=0; y<h-1; y++) {
        for (int x=0; x<w-1; x++) {
            count += process_area(x, y);
        }
    }
    
    int max_plus = 0;
    for (int y=0; y<h; y++) {
        for (int x=0; x<w; x++) {
            int mi = index_map_xy(x, y);
            if (plus_map_j[mi] > max_plus) { max_plus = plus_map_j[mi]; }
            if (plus_map_o[mi] > max_plus) { max_plus = plus_map_o[mi]; }
            if (plus_map_i[mi] > max_plus) { max_plus = plus_map_i[mi]; }
        }
    }
    
    cout << count + max_plus << endl;
    /*
    for (int y=0; y<h; y++) {
        for (int x=0; x<w; x++) {
            cout << map_xy(x,y);
        }
        cout << endl;
    }
        cout << endl;
    for (int y=0; y<h; y++) {
        for (int x=0; x<w; x++) {
            int n = plus_map_j[index_map_xy(x, y)];
            cout << n << " ";
        }
        cout << endl;
    }
        cout << endl;
    for (int y=0; y<h; y++) {
        for (int x=0; x<w; x++) {
            int n = plus_map_o[index_map_xy(x, y)];
            cout << n << " ";
        }
        cout << endl;
    }
        cout << endl;
    for (int y=0; y<h; y++) {
        for (int x=0; x<w; x++) {
            int n = plus_map_i[index_map_xy(x, y)];
            cout << n << " ";
        }
        cout << endl;
    }
    /*
    for (int y=0; y<h; iy++) {
        for (int x=0; x<w; ix++) {
            cout << map_xy(x,y);
        }
        cout << endl;
    }
    for (int iy=0; iy<2; iy++) {
        for (int ix=0; ix<2; ix++) {
            cout << emb_xy(ix,iy);
        }
        cout << endl;
    }
    */
    return 0;
}

Measured memory_bytes: 13856