#!/usr/bin/env python3 import sys from collections import defaultdict def parse(): ants = defaultdict(list) i = 0; for l in sys.stdin: for j in range(len(l.strip())): c = l[j] if c != '.': ants[c].append((i, j)) i += 1 return ants, i def onmap(i, j, n): return i >= 0 and i < n and j >=0 and j < n def get_anodes(a, b, n): dx = b[1] - a[1] dy = b[0] - a[0] cands = [ (b[0] + dy, b[1] + dx), (a[0] - dy, a[1] - dx) ] return filter(lambda x: onmap(x[0], x[1], n), cands) def count(ants, n): locs = set() for a in ants.keys(): nodes = ants[a] # pairwise step through and find new locs for i in range(len(nodes)): for j in range(i + 1, len(nodes)): for l in get_anodes(nodes[i], nodes[j], n): locs.add(l) return len(locs) if __name__ == '__main__': # input is square nxn ants, n = parse() print(count(ants, n))