Language: C++

#include<bits/stdc++.h>
using namespace std;

const int MOD = 100000;
class ModInt {
public:
  ModInt(int n = 0) : n_(n % MOD) {while(n_ < 0) n_ += MOD;}
  int value() const {return n_;}
  ModInt& operator+=(const ModInt& rhs);
private:
  int n_;
};
const ModInt operator+(const ModInt& lhs, const ModInt& rhs) {
  return lhs.value() + rhs.value();
}
const ModInt operator-(const ModInt& lhs, const ModInt& rhs) {
  return lhs.value() - rhs.value();
}
ModInt& ModInt::operator+=(const ModInt& rhs) {return *this = *this + rhs;}

bool good(const vector<string>& flag) {
  for(int i = 0; i < flag.size() - 1; ++i)
    for(int j = 0; j < flag[i].size() - 1; ++j)
      if(flag[i][j] == 'J' && flag[i][j+1] == 'O' && flag[i+1][j] == 'I')
        return true;
  return false;
}

int count(const vector<string>& flag, char c) {
  return accumulate(begin(flag), end(flag), 0, [=](int sum, const string& s){
      return sum + count(begin(s), end(s), c);
    });
}

int modpower(int x, int n, int mod) {
  if(!n) return 1;
  return n&1 ? x*modpower(x*x%mod, n>>1, mod)%mod : modpower(x*x%mod, n>>1, mod);
}

bool test(int bit, int i) {
  assert(0 <= i);
  return bit & (1 << i);
}

int set(int bit, int i) {
  assert(0 <= i);
  return bit | (1 << i);
}

int reset(int bit, int i) {
  assert(0 <= i);
  return bit & ((~0) ^ (1 << i));
}

int solve(const vector<string>& flag) {
  ModInt all = modpower(3, count(flag, '?'), MOD);
  if(good(flag)) return all.value();
  const int H = flag.size(), W = flag.front().size();
  const int BIT = 1 << W;
  vector<ModInt> cur(BIT);
  cur[0] = 1;
  for(int i = 0; i < H; ++i) for(int j = 0; j < W; ++j) {
    vector<ModInt> nex(BIT);
    for(int bit = 0; bit < BIT; ++bit) {
      int nbit = reset(bit, j);
      if(flag[i][j] == 'J' || flag[i][j] == '?') {
        if(j == W-1 || flag[i][j+1] != 'O' || i == H-1 || flag[i+1][j] != 'I') {
          if(j) nex[set(reset(nbit, j-1), j)] += cur[bit];
          else nex[set(nbit, j)] += cur[bit];
        }
      }
      if(flag[i][j] == 'O' || flag[i][j] == '?') {
        if(!j || i == H-1 || !test(bit, j-1) || flag[i+1][j-1] != 'I') {
          nex[nbit] += cur[bit];
        }
      }
      if(flag[i][j] == 'I' || flag[i][j] == '?') {
        if(!i || j == W-1 || !test(bit, j)) {
          if(j) nex[reset(nbit, j-1)] += cur[bit];
          else nex[nbit] += cur[bit];
        }
      }
    }
    cur = nex;
  }
  return (all - accumulate(begin(cur), end(cur), ModInt(0))).value();
}

int main() {
  int M, N;
  cin >> M >> N;
  vector<string> flag(M);
  for(auto& i: flag) cin >> i;
  cout << solve(flag) << endl;
}

Compiler output:
a.cc: In function 'int solve(const std::vector<std::__cxx11::basic_string<char> >&)':
a.cc:68:21: error: reference to 'set' is ambiguous
   68 |           if(j) nex[set(reset(nbit, j-1), j)] += cur[bit];
      |                     ^~~
In file included from /usr/include/c++/14/set:63,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:158,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_set.h:96:11: note: candidates are: 'template<class _Key, class _Compare, class _Alloc> class std::set'
   96 |     class set
      |           ^~~
a.cc:45:5: note:                 'int set(int, int)'
   45 | int set(int bit, int i) {
      |     ^~~
a.cc:69:20: error: reference to 'set' is ambiguous
   69 |           else nex[set(nbit, j)] += cur[bit];
      |                    ^~~
/usr/include/c++/14/bits/stl_set.h:96:11: note: candidates are: 'template<class _Key, class _Compare, class _Alloc> class std::set'
   96 |     class set
      |           ^~~
a.cc:45:5: note:                 'int set(int, int)'
   45 | int set(int bit, int i) {
      |     ^~~