#!/usr/bin/env python3 import sys G = [] # Safely get an item from the grid without out-of-bounds errs def get(i, j): if i >= 0 and i < len(G) and j >= 0 and j < len(G[0]): return G[i][j] return "" # Trick: rather than start the search on a M, which involves # making sure that there's a diagonal MAS counterpart # (which also involves avoiding double counting), just look # for the A in the middle thats shared! def xmas(i, j): if G[i][j] != "A": return 0 return ( (get(i+1, j+1) == "S" and get(i-1, j-1) == "M") or (get(i+1, j+1) == "M" and get(i-1, j-1) == "S") ) and ( (get(i-1, j+1) == "S" and get(i+1, j-1) == "M") or (get(i-1, j+1) == "M" and get(i+1, j-1) == "S") ) # Read the puzzle for l in sys.stdin: letts = [c for c in l.strip()] G.append(letts) c = 0 for i in range(len(G)): for j in range(len(G[0])): c += xmas(i, j) print(c)