#include "_.h"

int find_way(char **inputs, int y, int x, char before) {
    if (inputs[y][x] == '9') {
        inputs[y][x] = 'X';
        return 1;
    }
    
    int count = 0;
    if (inputs[y + 1] != NULL && inputs[y + 1][x] == before + 1)
        count += find_way(inputs, y + 1, x, before + 1);
    if (y > 0 && inputs[y - 1][x] == before + 1)
        count += find_way(inputs, y - 1, x, before + 1);
    if (x < (int)strlen(inputs[y]) && inputs[y][x + 1] == before + 1)
        count += find_way(inputs, y, x + 1, before + 1);
    if (x > 0 && inputs[y][x - 1] == before + 1)
        count += find_way(inputs, y, x - 1, before + 1);
    return count;
}

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

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

    int count = 0;
	for (int i = 0; inputs[i] != NULL; i++) {
        for (int j = 0; inputs[i][j] != '\0'; j++) {
            if (inputs[i][j] == '0')
                count += find_way(strsdup(inputs), i, j, '0');
        }
	}
    printf("count: %d\n", count);

	return 0;
}
