#include "_.h"

int strslen(char **strs) {
    int len = 0;
    for (int i = 0; strs[i] != NULL; i++)
        len++;

    return len;
}

int search (char **strs, int i, int j, int up_down, int left_right) {
    char to_find[4] = "";
    for (int incr = 0; incr < 4; incr++)
        if (strs[i + (up_down * incr)][j + (left_right * incr)] != to_find[incr])
            return 0;

    return 1;
}

int main(int argc, char **argv) {
	
	if (argc != 2)
		return 1;

	char *input = argv[1];
	char **inputs = str_split(strdup(input), '\n');
    int inputslen = strslen(inputs);

    int nbr = 0;
	for (int i = 0; inputs[i] != NULL; i++) {
        for (int j = 0; inputs[i][j] != '\0'; j++) {
            if (i <= 0) // Up 
                continue;
            if (j <= 0) // Up Left
                continue;
            if (j >= strlen(inputs[i]) - 1) // Up Right
                continue;
            if (i >= inputslen - 1) // Down
                continue;

            if (inputs[i][j] != 'A')
                continue;
            if ((inputs[i - 1][j - 1] == 'M' && inputs[i + 1][j + 1] == 'S') ||
                (inputs[i - 1][j - 1] == 'S' && inputs[i + 1][j + 1] == 'M')) {

                if ((inputs[i - 1][j + 1] == 'M' && inputs[i + 1][j - 1] == 'S') ||
                    (inputs[i - 1][j + 1] == 'S' && inputs[i + 1][j - 1] == 'M'))
                    nbr++;
            }

        }
	}

    printf("nbr: %d\n", nbr);

	return 0;
}
