#!/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] anodes = set() for loc in [a, b]: i, j = loc while onmap(i, j, n): anodes.add((i, j)) i += dy j += dx i, j = loc while onmap(i, j, n): anodes.add((i, j)) i -= dy j -= dx return anodes 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))