CF193A.Cutting Figure

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You've gotten an n×mn×m sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as AA . Set AA is connected. Your task is to find the minimum number of squares that we can delete from set AA to make it not connected.

A set of painted squares is called connected, if for every two squares aa and bb from this set there is a sequence of squares from the set, beginning in aa and ending in bb , such that in this sequence any square, except for the last one, shares a common side with the square that follows next in the sequence. An empty set and a set consisting of exactly one square are connected by definition.

输入格式

The first input line contains two space-separated integers nn and mm ( 1<=n,m<=501<=n,m<=50 ) — the sizes of the sheet of paper.

Each of the next nn lines contains mm characters — the description of the sheet of paper: the jj -th character of the ii -th line equals either "#", if the corresponding square is painted (belongs to set AA ), or equals "." if the corresponding square is not painted (does not belong to set AA ). It is guaranteed that the set of all painted squares AA is connected and isn't empty.

输出格式

On the first line print the minimum number of squares that need to be deleted to make set AA not connected. If it is impossible, print -1.

输入输出样例

  • 输入#1

    5 4
    ####
    #..#
    #..#
    #..#
    ####
    

    输出#1

    2
    
  • 输入#2

    5 5
    #####
    #...#
    #####
    #...#
    #####
    

    输出#2

    2
    

说明/提示

In the first sample you can delete any two squares that do not share a side. After that the set of painted squares is not connected anymore.

The note to the second sample is shown on the figure below. To the left there is a picture of the initial set of squares. To the right there is a set with deleted squares. The deleted squares are marked with crosses.

首页