#3429. [USACO2014 Jan] Building a Ski Course

[USACO2014 Jan] Building a Ski Course

Description

Farmer John is helping to turn his large field into a ski course for the upcoming winter Moolympics. The field has dimensions M×NM \times N (1M,N1001 \leq M,N \leq 100), and its intended final composition is described by an M×NM \times N grid of characters like this:

RSRSSS
RSRSSS
RSRSSS

Each character describes how the snow in a unit square of the field should be groomed: either R for rough or S for smooth (the Moolympics organizers think that a course is more interesting if it has a mixture of rough and smooth patches).

To build the desired course, Farmer John plans to modify his tractor so that it can stamp any B×BB \times B patch of the field (BMB \leq M, BNB \leq N) with either entirely smooth snow or entirely rough snow. Since it takes a long time to reset the tractor between each of these stamps, FJ wants to make BB as large as possible. With B=1B = 1, he can clearly create the desired ski course by stamping each individual square with either R or S, as intended. However, for larger values of BB, it may no longer be possible to create the desired course design. Every unit square of the course must at some point be stamped by FJ's tractor; it cannot be left in its default state.

Please help FJ determine the largest possible value of BB he can successfully use.

Input

  • Line 11: Two space-separated integers MM and NN.

  • Lines 2M+12 \sim M+1: MM lines of exactly NN characters (each R or S), describing the desired ski course design.

Output

  • Line 1: The maximum value of BB Farmer John can use to create the desired course pattern.
3 6
RSRSSS
RSRSSS
RSRSSS
3

Sample Explanation

FJ can stamp a rough patch spanning columns 131 \sim 3, followed by a smooth patch spanning columns 242 \sim 4, then a rough patch spanning columns 353 \sim 5, and finally a smooth patch spanning columns 464 \sim 6.