logo AlgoBeat OnlineJudge
登录 注册

#103429. [BZOJ 3429] [Usaco2014 Jan]Building a Ski Course

内存限制:128 MiB 时间限制:10000 ms 标准输入输出
题目类型:传统 评测方式:文本比较
上传者: 匿名

题目描述

Description

Farmer John is helping to turn his large field into a ski course for the upcoming winter Moolympics. The field has dimensions (), and its intended final composition is described by an 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 patch of the field (, ) 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 as large as possible. With , 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 , 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 he can successfully use.

Input

  • Line : Two space-separated integers and .

  • Lines : lines of exactly characters (each R or S), describing the desired ski course design.

Output

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

样例

样例输入 #1

3 6
RSRSSS
RSRSSS
RSRSSS

样例输出 #1

3

Sample Explanation

FJ can stamp a rough patch spanning columns , followed by a smooth patch spanning columns , then a rough patch spanning columns , and finally a smooth patch spanning columns .