Turning Off the Lights

Given a 10x10 grid of lit and unlit bulbs, find the minimum presses so that every bulb ends up off.

Medium6Brute forceBit manipulationGreedyImplementationInterviewNo attempts yetTime limit1sMemory limit128 MB

Problem

There are 100 bulbs arranged in a 10×1010 \times 10 grid. Pressing the switch of a bulb flips that bulb and the bulbs directly above, below, to the left, and to the right of it. A lit bulb turns off and an unlit bulb turns on. Positions outside the grid are not affected.

Given the initial state of the 100 bulbs, find the smallest number of switch presses that turns every bulb off.

Input

Ten lines are given, each with 10 characters. # is an unlit bulb and O (uppercase letter O) is a lit bulb. No character other than # and O appears in the input.

Output

Print the smallest number of switch presses needed to turn every bulb off, on one line. If turning every bulb off is impossible, print -1.