#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int check(int **map, int now, int notnow, int x, int y, int put)
{
    int toreru = 0;
    int temptoreru[3] = {0};
    int temp = 7;
    for(int i = 1; x-i >= 0 && temp != 0; i++)
    {
        if(temp & 0x04)
        {
            if(y-i < 0)
            {
                temp &= 0x03;
            }
            else
            {
                if(map[x-i][y-i] == now)
                {
                    if(put == 1)
                    {
                        for(int k = 0; k <= temptoreru[0]; k++)
                        {
                            map[x-k][y-k] = now;
                        }
                    }
                    toreru += temptoreru[0];
                    temp &= 0x03;
                }
                else if(map[x-i][y-i] == notnow)
                {
                    temptoreru[0]++;
                }
                else
                {
                    temp &= 0x03;
                }
            }
        }
        if(temp & 0x02)
        {
            if(map[x-i][y] == now)
            {
                if(put == 1)
                {
                    for(int k = 0; k <= temptoreru[1]; k++)
                    {
                        map[x-k][y] = now;
                    }
                }
                toreru += temptoreru[1];
                temp &= 0x05;
            }
            else if(map[x-i][y] == notnow)
            {
                temptoreru[1]++;
            }
            else
            {
                temp &= 0x05;
            }
        }
        if(temp & 0x01)
        {
            if(y+i >= 8)
            {
                temp &= 0x06;
            }
            else
            {
                if(map[x-i][y+i] == now)
                {
                    if(put == 1)
                    {
                        for(int k = 0; k <= temptoreru[2]; k++)
                        {
                            map[x-k][y+k] = now;
                        }
                    }
                    toreru += temptoreru[2];
                    temp &= 0x06;
                }
                else if(map[x-i][y+i] == notnow)
                {
                    temptoreru[2]++;
                }
                else
                {
                    temp &= 0x06;
                }
            }
        }
    }

    temp = 0x07;
    temptoreru[0] = 0;
    temptoreru[1] = 0;
    temptoreru[2] = 0;

    for(int i = 1; x + i < 8 && temp != 0; i++)
    {
        if(temp & 0x04)
        {
            if(y-i < 0)
            {
                temp &= 0x03;
            }
            else
            {
                if(map[x+i][y-i] == now)
                {
                    if(put == 1)
                    {
                        for(int k = 0; k <= temptoreru[0]; k++)
                        {
                            map[x+k][y-k] = now;
                        }
                    }
                    toreru += temptoreru[0];
                    temp &= 0x03;
                }
                else if(map[x+i][y-i] == notnow)
                {
                    temptoreru[0]++;
                }
                else
                {
                    temp &= 0x03;
                }
            }
        }
        if(temp & 0x02)
        {
            if(map[x+i][y] == now)
            {
                if(put == 1)
                {
                    for(int k = 0; k <= temptoreru[1]; k++)
                    {
                        map[x+k][y] = now;
                    }
                }
                toreru += temptoreru[1];
                temp &= 0x05;
            }
            else if(map[x+i][y] == notnow)
            {
                temptoreru[1]++;
            }
            else
            {
                temp &= 0x05;
            }
        }
        if(temp & 0x01)
        {
            if(y+i >= 8)
            {
                temp &= 0x06;
            }
            else
            {
                if(map[x+i][y+i] == now)
                {
                    if(put == 1)
                    {
                        for(int k = 0; k <= temptoreru[2]; k++)
                        {
                            map[x+k][y+k] = now;
                        }
                    }
                    toreru += temptoreru[2];
                    temp &= 0x06;
                }
                else if(map[x+i][y+i] == notnow)
                {
                    temptoreru[2]++;
                }
                else
                {
                    temp &= 0x06;
                }
            }
        }
    }

    temp = 0x03;
    temptoreru[0] = 0;
    temptoreru[1] = 0;

    for(int i = 1; temp != 0; i++)
    {
        if(temp & 0x02)
        {
            if(y-1 < 0)
            {
                temp &= 0x01;
            }
            else
            {
                if(map[x][y-i] == now)
                {
                    if(put == 1)
                    {
                        for(int k = 0; k <= temptoreru[0]; k++)
                        {
                            map[x][y-k] = now;
                        }
                    }
                    toreru += temptoreru[0];
                    temp &= 0x01;
                }
                else if(map[x][y-i] == notnow)
                {
                    temptoreru[0]++;
                }
                else
                {
                    temp &= 0x01;
                }
            }
        }
        if(temp & 0x01)
        {
            if(y+1 >= 8)
            {
                temp &= 0x02;
            }
            else
            {
                if(map[x][y+i] == now)
                {
                    if(put == 1)
                    {
                        for(int k = 0; k <= temptoreru[1]; k++)
                        {
                            map[x][y+k] = now;
                        }
                    }
                    toreru += temptoreru[1];
                    temp &= 0x02;
                }
                else if(map[x][y+i] == notnow)
                {
                    temptoreru[1]++;
                }
                else
                {
                    temp &= 0x02;
                }
            }
        }
    }

    return toreru;
}

int main()
{
    //int map[8][8] = {{0}};
    int **map;
    map = (int **)malloc(sizeof(int *)*8);
    char temp;
    for(int i = 0; i < 8; i++)
    {
        map[i] = (int *)malloc(sizeof(int)*8);
        for(int j = 0; j < 8; j++)
        {
            scanf("%c", &temp);
            if(temp == 'o') map[i][j] = 1;
            else if(temp == 'x') map[i][j] = 2;
            else map[i][j] = 0;
        }
        scanf("%c", &temp);
    }

    int turn = 1;
    int loop = 0;

    while(loop != 2)
    {
        //printf("%d, %d\n", turn, loop);
        if(turn == 1)
        {
            int max = 0;
            int x = -1;
            int y = -1;
            for(int i = 0; i < 8; i++)
            {
                for(int j = 0; j < 8; j++)
                {
                    if(map[i][j] == 0)
                    {
                        int temp = check(map, 1, 2, i, j, 0);
                        if(temp > max)
                        {
                            max = temp;
                            x = i;
                            y = j;
                        }
                    }
                }
            }
            if(x == -1)
            {
                loop++;
            }
            else
            {
                //printf("%d, %d\n", x, y);
                int temp = check(map, 1, 2, x, y, 1);
                loop = 0;
            }
            turn = 2;
        }
        else
        {
            int max = 0;
            int x = -1;
            int y = -1;
            for(int i = 7; i >= 0; i--)
            {
                for(int j = 7; j >= 0; j--)
                {
                    if(map[i][j] == 0)
                    {
                        int temp = check(map, 2, 1, i, j, 0);
                        if(temp > max)
                        {
                            max = temp;
                            x = i;
                            y = j;
                        }
                    }
                }
            }
            if(x == -1)
            {
                loop++;
            }
            else
            {
                //printf("%d, %d\n", x, y);
                int temp = check(map, 2, 1, x, y, 1);
                loop = 0;
            }
            turn = 1;
        }

        /*
        for(int i = 0; i < 8; i++)
        {
            for(int j = 0; j < 8; j++)
            {
                if(map[i][j] == 0) printf(".");
                if(map[i][j] == 1) printf("o");
                if(map[i][j] == 2) printf("x");
            }
            printf("\n");
        }//*/
    }


    for(int i = 0; i < 8; i++)
    {
        for(int j = 0; j < 8; j++)
        {
            if(map[i][j] == 0) printf(".");
            if(map[i][j] == 1) printf("o");
            if(map[i][j] == 2) printf("x");
        }
        printf("\n");
    }

    return 0;
}


Measured memory_bytes: 2692