Polyomino Powers

Time limit1sMemory limit128 MB

Summary
Given a polyomino on a grid up to 10x10, find the smallest k in 2..5 such that the shape is tiled by k translated copies of one smaller polyomino, or report none.
Level

Hard8 of 10

Topics
Brute force, Backtracking, Implementation, Geometry
Solved
No attempts yet

Problem

A polyomino is a shape whose basic building block is the unit square. It is a connected figure formed by joining one or more identical squares placed at distinct positions on the regular square grid, so that every square is connected to every other square through a chain of shared edges (shapes joined only at corners are not allowed). The best-known polyominoes are the seven tetrominoes made of four squares (famous from the game Tetris) and the domino made of two squares.

Some polyominoes can be built by taking several copies of a single smaller polyomino and gluing them — using translation only, without rotation or reflection — at different positions in the plane. Such a polyomino is called a power of the smaller one. Formally, a polyomino is a kk-power if it can be exactly covered, without overlaps, by kk translated copies of one smaller polyomino.

Input

The first line contains two positive integers hh and ww (h,w≤10h, w \le 10).

Each of the next hh lines contains ww characters describing an h×wh \times w grid. Each character is either . or X; an X marks a cell that belongs to the polyomino and a . marks empty space. The X cells form a single polyomino (they are edge-connected).

Output

Print the smallest integer kk with 2≤k≤52 \le k \le 5 such that the given polyomino is a kk-power — that is, the smallest number of translated copies of one smaller polyomino that exactly cover it. If no such kk exists, print No solution instead.

Examples4

  1. Example 1

    Input
    4 9
    .X..X.X.X
    .XX.X.X.X
    XXXXXXXXX
    .XX......
    
    Expected output
    5
    
  2. Example 2

    Input
    3 7
    .XXXXX.
    .XX..X.
    XXXX...
    
    Expected output
    No solution
    
  3. Example 3

    Input
    1 3
    XXX
    
    Expected output
    3
    
  4. Example 4

    Input
    1 4
    XXXX
    
    Expected output
    2