extension String: CollectionType {} // splitを使うためのおまじない

let a: [Int] = readLine()!.split(" ").map { Int($0)! }
var map: [[Character]] = []
var nmap: [[Character]] = []

func setSharp(x: Int, y: Int, max_x: Int, max_y: Int, list: [[Character]] ) {
  if( x >= 0  && x < max_x && y >= 0 && y < max_y ) {
  	nmap[x][y] = "#"
  }
}
func setBlack(x: Int, y: Int, max_x: Int, max_y: Int, list: [[Character]] ) {
  setSharp(x-1, y: y, max_x: max_x, max_y: max_y, list: list )
  setSharp(x+1, y: y, max_x: max_x, max_y: max_y, list: list )
  setSharp(x, y: y-1, max_x: max_x, max_y: max_y, list: list )
  setSharp(x, y: y+1, max_x: max_x, max_y: max_y, list: list )
  /*
  nmap[x-1][y] = "#"
  nmap[x+1][y] = "#"
  nmap[x][y-1] = "#"
  nmap[x][y+1] = "#" 
  */
} 

for var i = 0; i < a[0]; i += 1 {
  map.append([])
  var input_line: String? = readLine() 
  //print( input_line )
  for num in input_line!.characters {
    map[i].append(num)
  }
  
}

var count = 0;
nmap = map
var all_black: Int = 0
while( all_black == 0 ) {

    all_black = 1
for var i = 0; i < a[0]; i += 1 {

  var input_line: String? = readLine() 
  //print( input_line )
  for var j = 0; j < a[1]; j += 1 {
    if( map[i][j] == "#" ){
      setBlack(i, y:j, max_x: a[0], max_y: a[1], list: nmap)
    }
    else {
      if( all_black == 1 ) {
        all_black = 0
        count += 1
      }
    }
  }
}
  map = nmap

}

print( count )
