Under construction

Still being built. Things may change

Back to Space

Time limit2sMemory limit512 MB

Summary
Given an r by c grid of colored tiles, find a panel size a by b and a row shift s so that the grid is covered by shifted copies of the panel, with minimal panel area.
Level

Hard9 of 10

Topics
String matching, Hash map, Math
Solved
No attempts yet

Statement

For the 50th anniversary of the first crewed spaceflight, a new reusable spacecraft called "Vostorg" is being built. The rectangular part of its hull (called the rectangle below) must be covered with square heat-shield tiles of one size and several colors. The rectangle has rr rows of cc tiles each. The tiles must form a given pattern.

Laying tiles one at a time is too laborious, so the pattern is built from identical rectangular panels made of tiles. Panels are attached one after another, filling the rectangle row by row from top to bottom. Each row of panels may be shifted to the right by the same number of tiles relative to the previous row. Panels may extend beyond the rectangle. All panels must have the same orientation: when one panel is translated onto another, the colors of their tiles must match.

The chief designer wants to pick a panel size a×ba \times b and a shift ss so that the pattern can be laid out with these panels, and the panel area is as small as possible. Here 0≤s<b0 \le s < b is the shift to the right of each next row relative to the previous one.

An example of panels with a=2a = 2, b=3b = 3, s=1s = 1.

Input

The first line contains two integers rr and cc, the size of the rectangle in tiles. The next rr lines give the tile colors. Each of the k≤26k \le 26 colors is one of the first kk uppercase Latin letters. It is guaranteed that a panel of size a×ba \times b exists for this rectangle with 2a≤r2a \le r and 2b≤c2b \le c.

Output

Print three integers aa, bb, and ss that satisfy the conditions. If several solutions exist, print any of them.

Limits

The shift ss has no restriction. Both rr and cc do not exceed 1961.

Hint

The tiling for the second example is shown below. Parts of the panels that stick out past the rectangle are not drawn.

Examples2

  1. Example 1

    Input
    2 4
    ABAB
    ABAB
    
    Expected output
    1 2 0
    
  2. Example 2

    Input
    5 7
    DCADCAD
    BABBABB
    ADCADCA
    BBABBAB
    CADCADC
    
    Expected output
    2 3 1