John and Jack, two mathematicians, created a game called “Bomb Game” at spared time. This game is played on an n*m chessboard. A pair of integers (p, q) represents the grid at row p, column q. Some bombs were placed on the chessboard at the beginning. Every round, a player can choose to explode a bomb located at (p, q), and the exploded bomb will disappear. Furthermore: 

1.If p>1 and q>1, the bomb will split up into two bombs located at (u, q) and (p, v), u<p, v<q, u and v are chosen by the player. 
2.If p=1 and q>1, one new bomb will be produced, located at (p, v), v<q, v can be chosen freely by the player. 
3.If q=1 and p>1, one new bomb will be produced, located at (u, q), u<p, u can be chosen freely by the player. 


If two bombs located at the same position or a bomb located at (1, 1), they will be exploded automatically without producing new bombs. 
Two players play in turn, until one player cannot explode the bombs and loses the game. 
John always plays first. 
Now, we’ll give you an initial situation, and you should tell us who will win at last. Assume John and Jack are smart enough, and they always do the best choice.

Input

There are several test cases, each one begins with two integers n and m, 0<n, m<=50, represents the number of rows and columns. Following by an n*m grid, describing the initial situation, ‘#’ indicates bomb. 
The input is terminated by n=m=0.

Output

For each test case, output one line, the name of the winner.

Sample Input

2 2
.#
..
2 2
.#
.#
0 0

Sample Output

John
Jack

题意:#是炸弹 ,引爆炸弹会在左侧和上侧产生新的炸弹  1,1位置的炸弹无法被引爆(左侧上侧都是边界) 两个炸弹合在一起也会爆炸 ,谁不能引爆谁输

题解:二维sg函数打表 然后就是nim游戏了

#include <bits/stdc++.h>
#define maxn 1000+5
#define INF 0x3f3f3f3f
using namespace std;
int sg[maxn][maxn];//0~n的SG函数值
int Hash[maxn];
int getsg(int a,int b){
    memset(Hash,0,sizeof(Hash));
    for(int i=0;i<a;i++)
        for(int j=0;j<b;j++){
            Hash[sg[a][j]^sg[i][b]]=1;
        }
    for(int i=0;;i++){
        if(!Hash[i])
        return i;
    }
}
int main(){
    memset(sg,-1,sizeof(sg));
    for(int i=0;i<55;i++)sg[i][0]=sg[0][i]=i;
    for(int i = 1; i < 55; i ++) {
        for(int j = 1; j < 55; j ++) {
            sg[i][j] = getsg(i,j);
        }
    }
    int n,m;
     while(scanf("%d%d",&n,&m)!=EOF&&n+m){
        getchar();
        int ans=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                char c=getchar();
                if(c=='#'){
                    ans^=sg[i][j];
                }
            }
            getchar();
        }
        if(ans)printf("John\n");
        else printf("Jack\n");
    }
    return 0;
}