MOD = 998244353i64 FACT = [1i64] 100000.times do |i| FACT << FACT[i] * (i + 1) % MOD end FACT_INV = Array.new(FACT.size, 0i64) FACT_INV[-1] = inv(FACT[-1]) (FACT_INV.size - 2).downto(0) do |i| FACT_INV[i] = FACT_INV[i + 1] * (i + 1) % MOD end def binom(n, k) FACT[n] * FACT_INV[k] % MOD * FACT_INV[n - k] % MOD end def inv(v : Int64) pow(v, MOD - 2) end def pow(v : Int64, p) ret = 1i64 while p > 0 if (p & 1i64) != 0 ret *= v ret %= MOD end v *= v v %= MOD p >>= 1 end ret end n, m = read_line.split.map(&.to_i) s = Array.new(n) { read_line.chars } cnt_b = Array.new(3, 0) cnt_q = Array.new(3, 0) n.times do |i| m.times do |j| if s[i][j] == 'B' cnt_b[(i + j) % 3] += 1 elsif s[i][j] == '?' cnt_q[(i + j) % 3] += 1 end end end ans_e = 1i64 ans_o = 1i64 3.times do |i| sum_e = 0i64 sum_o = 0i64 0.upto(cnt_q[i]) do |j| if j % 2 == cnt_b[i] % 2 sum_e += binom(cnt_q[i], j) else sum_o += binom(cnt_q[i], j) end end ans_e *= sum_e % MOD ans_e % MOD ans_o *= sum_o % MOD ans_o % MOD end puts (ans_e + ans_o) % MOD