Flatten the grid into a single string (separated by line-feeds to
side-step wrap-around problems) and use the offset in the string
to identify cells instead of (x, y) tuples.
Don't calculate part one separately, just do it as we go.
I think only two repeats of the reductions are ever needed,
so ditch tracking whether the counts are modified or not.
Golf away a couple of other minor lines.