看起来很神,但只有三种情况 -1 , 1 ,2.....
/**
* Created by ckboss on 14-10-8.
*/
import java.util.*;
public class CuttingFigure {
static int n,m;
static int[] dir_x = {0,0,1,-1};
static int[] dir_y = {1,-1,0,0};
static boolean[][] vis = new boolean[55][55];
static char[][] map = new char[55][55];
static boolean inmap(int x,int y){
return (x>=0&&x<n)&&(y>=0&&y<m);
}
static void dfs(int x,int y){
vis[x][y]=true;
for(int i=0;i<4;i++){
int X=dir_x[i]+x;
int Y=dir_y[i]+y;
if(inmap(X,Y)&&vis[X][Y]==false&&map[X][Y]=='#'){
dfs(X,Y);
}
}
}
static int CountNum(int x,int y){
for(int i=0;i<55;i++) Arrays.fill(vis[i],false);
if(inmap(x,y)) vis[x][y]=true;
int cnt=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(vis[i][j]==false&&map[i][j]=='#'){
dfs(i,j);
cnt++;
}
}
}
return cnt;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
n=in.nextInt(); m=in.nextInt();
int nb=0;
in.nextLine();
for(int i=0;i<n;i++){
String line = in.nextLine();
for(int j=0;j<m;j++){
map[i][j]=line.charAt(j);
if(map[i][j]=='#') nb++;
}
}
if(nb<3){
System.out.println("-1");
return ;
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(CountNum(i,j)==1){
continue;
}
else {
System.out.println("1");
return ;
}
}
}
System.out.println("2");
}
}