#include <iostream>
#include <vector>
#include <unordered_map>
#include <bitset>
#include <cstring>

constexpr uint32_t p = 998244353;
typedef uint32_t *flagmap;

std::vector<uint32_t> powersof3;

int digitof(uint32_t hash, int idx)
{
    return (hash / powersof3[idx]) % 3;
}

void setdigit(uint32_t &hash, int idx, int newval)
{
    hash -= digitof(hash, idx) * powersof3[idx];
    hash += newval * powersof3[idx];
}

bool isflag(int a, int b, int c, int d)
{
    return a == 0 && b == 1 && c ==2 && d == 1;
}

void populateNoFlags(const std::vector<uint32_t> &grid, flagmap *fm, int cur, int m, uint32_t hash)
{
    if(cur == m)
    {
        fm[0][hash] = 1;
        return;
    }

    if(grid[cur] == 3)
    {
        for(uint32_t j=0; j<3; j++)
        {   
            uint32_t newhash = hash;
            setdigit(newhash, cur, j);
            populateNoFlags(grid, fm, cur+1, m, newhash);
        }
    }
    else
    {
        setdigit(hash, cur, grid[cur]);
        populateNoFlags(grid, fm, cur+1, m, hash);
    }
}

int main()
{
    int n, m;
    std::cin >> n >> m;
    int questionmarks = 0;
    std::vector<uint32_t> grid(n*m);
    for(int i=0; i<n*m; i++)
    {
        char c;
        std::cin >> c;
        if(c == 'I')
            grid[i] = 0;
        else if(c == 'C')
            grid[i] = 1;
        else if(c == 'P')
            grid[i] = 2;        
        else
        {
            grid[i] = 3;
            questionmarks++;
        }
    }
    
    uint32_t base = 1;
    for(int i=0; i<m; i++)
    {
        powersof3.push_back(base);
        base = base *3;
    }
    
    uint32_t mapsize = base;
    
    flagmap noflags[3];
    flagmap newnoflags[3];
    for(int i=0; i<3; i++)
    {
        noflags[i] = new uint32_t[mapsize];
        newnoflags[i] = new uint32_t[mapsize];
    }
    
    for(int i=0; i<3; i++)
    {
        std::memset(noflags[i], 0, mapsize * sizeof(int32_t));
    }

    populateNoFlags(grid, noflags, 0, m, 0);
    
    for(int row=1; row<n; row++)
    {
        {
            for(int i=0; i<3; i++)
            {
                std::memset(newnoflags[i], 0, mapsize * sizeof(int32_t));
            }

            for(int prev=0; prev<3; prev++)
            {
                for(uint32_t hash = 0; hash < mapsize; hash++)
                {
                    for(uint32_t j = 0; j<3; j++)
                    {
                        uint32_t newhash = hash;
                        setdigit(newhash, 0, j);
                        int newprev = digitof(hash, 0);
                        if(grid[row*m] == j || grid[row*m] == 3)
                        {
                            newnoflags[newprev][newhash] = (newnoflags[newprev][newhash] + noflags[prev][hash]) % p;
                        }                       
                    }
                }
            }
            for(int i=0; i<3; i++)
            {
                std::swap(noflags[i], newnoflags[i]);
            }
        }
        for(int col = 1; col < m; col++)
        {
            for(int i=0; i<3; i++)
            {
                std::memset(newnoflags[i], 0, mapsize * sizeof(int32_t));
            }


            for(int prev=0; prev<3; prev++)
            {
                for(uint32_t hash = 0; hash < mapsize; hash++)
                {
                    for(uint32_t j = 0; j<3; j++)
                    {            
                        uint32_t newhash = hash;
                        setdigit(newhash, col, j);
                        int newprev = digitof(hash, col);
                        if(grid[row*m+col] == j || grid[row*m+col] == 3)
                        {
                            if(!isflag(prev, newprev, digitof(hash, col-1), j))
                            {
                                newnoflags[newprev][newhash] = (newnoflags[newprev][newhash] + noflags[prev][hash]) % p;
                            }
                        }
                    }
                }
            }
            for(int i=0; i<3; i++)
            {
                std::swap(noflags[i], newnoflags[i]);
            }
        }
    }
    uint32_t tot = 1;
    for(int i=0; i<questionmarks; i++)
    {
        tot = (tot * 3) % p;
    }
    uint32_t finalflags = 0;
    for(int j=0; j<3; j++)
    {
        for(uint32_t hash = 0; hash < mapsize; hash++)
        {
            finalflags = (finalflags + noflags[j][hash]) % p;
        }
    }
    
    uint32_t finalanswer = (tot + p - finalflags) % p;
    
    std::cout << finalanswer << std::endl;
}
