Skip to content

Instantly share code, notes, and snippets.

@avamsi
Created August 19, 2016 19:22
Show Gist options
  • Save avamsi/8544cf7d698e4247920db129694d48bb to your computer and use it in GitHub Desktop.
Save avamsi/8544cf7d698e4247920db129694d48bb to your computer and use it in GitHub Desktop.
import re
raw_input = lambda: ''.join(input().split())
m = int(raw_input())
n = int(raw_input())
rows = [raw_input() for _ in range(m)]
cols = [''.join([rows[i][j] for i in range(m)]) for j in range(n)]
dgls = [''.join([rows[i][i - j] for i in range(j, min(m, n + j))]) for j in range(m)]
dgls.extend(''.join([rows[i][i + j] for i in range(min(m, n - j))]) for j in range(1, n))
patt = raw_input()
regx = re.compile('(?=%s)' % patt)
print(sum(len(regx.findall(row)) for row in rows) +
sum(len(regx.findall(col)) for col in cols) +
sum(len(regx.findall(dgl)) for dgl in dgls))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment