Language: C++

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string>
#include <tuple>
#include <vector>
#include <map>
#include <unordered_map>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <cassert>
using namespace std;
#define LL long long
#define MP(a, b) make_pair(a, b)
#define MMP(a, b, c) make_pair(make_pair(a, b), c)
#define MAX 1000000000
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define LL_MIN -9223372036854775808
#define LL_MAX 9223372036854775807
#define PI 3.14159265359

int n,m,x,y,ie,bit,l;
int mp[10][10];
map<pair<int,int>,int> memo;
map<pair<int,int>,int> num;
int dfs(int I, int J){
    if(l != -1 && num.count(MP(bit,l)) != 0 return memo[MP(bit,l)];

    if(bit == ie){
        if(I != x && J != y) return 0;
        int ans = 0;
        for(int i=I-1; i>=0; i--){
            if(mp[i][J] == -1) break;
            if(mp[i][J] == 2) ans++;
        }
        for(int i=I+1; i<n; i++){
            if(mp[i][J] == -1) break;
            if(mp[i][J] == 2) ans++;
        }
        for(int j=J-1; j>=0; j--){
            if(mp[I][j] == -1) break;
            if(mp[I][j] == 2) ans++;
        }
        for(int j=J+1; j<m; j++){
            if(mp[I][j] == -1) break;
            if(mp[I][j] == 2) ans++;
        }
        return memo[MP(bit,l)] = ans;
    }

    int ans = 0;
    int L = l;
    for(int i=I-1; i>=0; i--){
        if(mp[i][J] == -1) break;
        if(mp[i][J] == 1){
            mp[i][J] *= -1;
            bit = bit ^ (1<<num[MP(i,J)]);
            l = num[MP(i,J)];
            ans += dfs(i,J);
            mp[i][J] *= -1;
            bit = bit ^ (1<<num[MP(i,J)]);
        }
    }
    for(int i=I+1; i<n; i++){
        if(mp[i][J] == -1) break;
        if(mp[i][J] == 1){
            mp[i][J] *= -1;
            bit = bit ^ (1<<num[MP(i,J)]);
            l = num[MP(i,J)];
            ans += dfs(i,J);
            mp[i][J] *= -1;
            bit = bit ^ (1<<num[MP(i,J)]);
        }
    }
    for(int j=J-1; j>=0; j--){
        if(mp[I][j] == -1) break;
        if(mp[I][j] == 1){
            mp[I][j] *= -1;
            bit = bit ^ (1<<num[MP(I,j)]);
            l = num[MP(I,j)];
            ans += dfs(I,j);
            mp[I][j] *= -1;
            bit = bit ^ (1<<num[MP(I,j)]);
        }
    }
    for(int j=J+1; j<m; j++){
        if(mp[I][j] == -1) break;
        if(mp[I][j] == 1){
            mp[I][j] *= -1;
            bit = bit ^ (1<<num[MP(I,j)]);
            l = num[MP(I,j)];
            ans += dfs(I,j);
            mp[I][j] *= -1;
            bit = bit ^ (1<<num[MP(I,j)]);
        }
    }

    return memo[MP(bit,l)] = ans;
}

int main(){
    iostream::sync_with_stdio(false);
    
    while(true){
        cin >> m >> n;
        if(n == 0 && m == 0) break;

        num.clear();
        ie = 0;
        bit = 0;
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                cin >> mp[i][j];
                if(mp[i][j] == 2){
                    x = i;
                    y = j;
                }
                if(mp[i][j] == 1){
                    num[MP(i,j)] = ie;
                    ie++;
                }
            }
        }
        ie = (1<<ie) - 1;
        l = -1;
        memo.clear();

        cout << dfs(x,y) << endl;
    }
    
    return 0;
}


Compiler output:
a.cc: In function 'int dfs(int, int)':
a.cc:37:44: error: expected ';' before 'return'
   37 |     if(l != -1 && num.count(MP(bit,l)) != 0 return memo[MP(bit,l)];
      |                                            ^~~~~~~
      |                                            ;
a.cc:39:5: error: expected primary-expression before 'if'
   39 |     if(bit == ie){
      |     ^~
a.cc:37:68: error: expected ')' before 'if'
   37 |     if(l != -1 && num.count(MP(bit,l)) != 0 return memo[MP(bit,l)];
      |       ~                                                            ^
      |                                                                    )
   38 | 
   39 |     if(bit == ie){
      |     ~~                                                              
a.cc:69:13: error: 'ans' was not declared in this scope; did you mean 'abs'?
   69 |             ans += dfs(i,J);
      |             ^~~
      |             abs
a.cc:80:13: error: 'ans' was not declared in this scope; did you mean 'abs'?
   80 |             ans += dfs(i,J);
      |             ^~~
      |             abs
a.cc:91:13: error: 'ans' was not declared in this scope; did you mean 'abs'?
   91 |             ans += dfs(I,j);
      |             ^~~
      |             abs
a.cc:102:13: error: 'ans' was not declared in this scope; did you mean 'abs'?
  102 |             ans += dfs(I,j);
      |             ^~~
      |             abs
a.cc:108:30: error: 'ans' was not declared in this scope; did you mean 'abs'?
  108 |     return memo[MP(bit,l)] = ans;
      |                              ^~~
      |                              abs