Problem: Example

Input

2 2
..
..


Output

Second

Solution (in CPP):

Solution:
#include "bits/stdc++.h"

using namespace std;

int H, W;
vector<string> s(20);
int dp[20][20][20][20];

int grundy(int sx, int sy, int gx, int gy)
{
    if (gx < sx || gy < sy)
        return 0;
    if (dp[sx][sy][gx][gy] >= 0)
        return dp[sx][sy][gx][gy];

    set<int> st;
    for (int i = sx; i <= gx; i++)
    {
        for (int j = sy; j <= gy; j++)
        {
            if (s[i][j] == '.')
            {
                st.insert(grundy(sx, sy, i - 1, j - 1) ^ grundy(i + 1, sy, gx, j - 1) ^ grundy(sx, j + 1, i - 1, gy) ^ grundy(i + 1, j + 1, gx, gy));
            }
        }
    }
    int res = 0;
    while (st.find(res) != st.end())
        res++;
    return dp[sx][sy][gx][gy] = res;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> H >> W;
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < H; i++)
        cin >> s[i];
    cout << (grundy(0, 0, H - 1, W - 1) ? "First" : "Second") << endl;
}