attrs = Array.new
while line = $stdin.gets
  attrs << line.chomp.split
end

matrix = Array.new(attrs[0][0].to_i) { Array.new(attrs[0][1].to_i)}

white = 0
for row in 0..matrix.size-1
    for column in 0..matrix[0].size-1
        matrix[row][column] = attrs[row+1][0][column]
        white += 1 if attrs[row+1][0][column] == '.'
    end
end

def create_dark(matrix, remaining_white, count)
    return count if remaining_white == 0
    
    for row in 0..matrix.size-1
        for col in 0..matrix[0].size-1
            next if ['.', '!'].include? matrix[row][col]
            
            for i in 0..matrix.size-1
                next if matrix[i][col] == '#'
                matrix[i][col] = '!'
                remaining_white -= 1
            end

            for j in 0..matrix[0].size-1
                next if matrix[row][j] == '#'
                matrix[row][j] = '!'
                remaining_white -= 1
            end
        end
    end

    count += 1

    create_dark(matrix, remaining_white, count)
end

p create_dark(matrix, white, 0)