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

map<int,pair<int,int> > Mp;
map<int,int> nex;
// -1 for O, 1 for x, 0 for empty

bool winning(int a[3][3], int type){
    //check hor rows
    for (int i=0; i<3; i++){
        bool res = true;
        for (int j=0; j<3; j++){
            if (a[i][j]!=type){
                res = false; break;
            }
        }
        if (res) {return true;}
    }
    //check ver cols
    for (int i=0; i<3; i++){
        bool res = true;
        for (int j=0; j<3; j++){
            if (a[j][i]!=type){
                res = false; break;
            }
        }
        if (res) {return true;}
    }
    //check two dias
    bool left = true;
    bool right = true;
    for (int i=0; i<3; i++){
        if (a[i][i]!=type){left = false;}
        if (a[i][2-i]!=type){right = false;}
    }
    return (left|right);
}

bool draw(int a[3][3], int type){
    for (int i=0; i<3; i++){
        for (int j=0; j<3; j++){
            if (a[i][j]==0){return false;}
        }
    }
    return (!winning(a,type));
}

bool losing(int a[3][3], int type){
    return winning(a,nex[type]);
}

int getnew(int a[3][3], int na[3][3], int tomove, int id){
    for (int i=0; i<3; i++){
        for (int j=0; j<3; j++){
            na[i][j] = a[i][j];
        }
    }
    pair<int,int> xy = Mp[id];
    int xi = xy.first, yj = xy.second;
    if (na[xi][yj]!=0){return false;}
    na[xi][yj] = tomove;
    return true;
}

bool print(int a[3][3]){
    for (int i=0; i<3; i++){
        for (int j=0; j<3; j++){
            cout<<a[i][j]<<" ";
        }
        cout<<"\n";
    }
    return true;
}

///1 for win, 0 for draw, -1 for loss
int nodes;

pair<int,int> findmove(int a[3][3], int tomove){
    nodes++;
    if (winning(a,-1)){
        return make_pair(1,-1);
    }
    if (losing(a,-1)){
        return make_pair(-1,-1);
    }
    if (draw(a,-1)){
        return make_pair(0,-1);
    }

    int best = -1;
    int nbest = -1;
    int gstat = -1;
    if (tomove==1){
        gstat = 1;
    }
    for (int i=0; i<9; i++){
        int na[3][3];
        int flag = getnew(a,na,tomove,i);
        if (flag==0){continue;}
        pair<int,int> getstatus = findmove(na, nex[tomove]);
        int status = getstatus.first;

        if (status==1){
            if (tomove==-1){
                best = i;
                gstat = 1;
            }
            continue;
        }
        if (status==0){
            if (tomove==-1){
                if (nbest==-1){
                    nbest = i;
                }
                if (gstat<0){
                    gstat = 0;
                }
            }
            if (tomove==1){
                if (gstat>0){
                    gstat = 0;
                }
            }
            continue;
        }
        if (status==-1){
            if (tomove==1){
                gstat = -1;
            }
            continue;
        }
    }
    if (gstat==1){
        return make_pair(1,best);
    }
    if (gstat==0){
        if (best!=-1){
            nbest = best;
        }
        return make_pair(0,nbest);
    }
    return make_pair(-1,-1);
}

int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    nex[-1] = 1;
    nex[1] = -1;
    pair<int,int> p;
    for (int i=0; i<9; i++){
        p.first = i/3;
        p.second = i%3;
        Mp[i] = p;
    }
    int a[3][3];
    for (int i=0; i<3; i++){
        for (int j=0; j<3; j++){
            cin>>a[i][j];
        }
    }
    cout<<"Current board\n";
    print(a);
    p = findmove(a,-1);
    cout<<"Minimax returns "<<p.first<<" "<<p.second<<endl;
    if (p.first==-1){cout<<"AI resigns, all moves are losing\n"; return 0;}
    if (p.first==1){cout<<"AI thinks that it will win\n";}
    if (p.first==0){cout<<"AI thinks that it will be draw if played optimally\n";}
    p = Mp[p.second];
    a[p.first][p.second] = -1;
    cout<<"Nodes scanned = "<<nodes<<"\n";
    nodes = 0;
    cout<<"Board after AI move\n";
    print(a);
}
