Andrew , there are lot of issues in this code.
1. I assume ma is of type
String and if this is String then you should not do ma =="ALL". ma and "ALL" will never be same as in case of Strings == will check for the object reference and not the value.
2. You are not considering the case when ma does not contain 'fire' and ma is not equal to "ALL", so that means the variable S will never be set to 1 and even if the for loop ends after some pre defined iterations you are again starting the for loop as the S is still 0 and as you are ierating over the same previous data set which does not match your condition this will keep looping.
Please modify your conditions to consider all the scenarios and ideally you should always have a else for the if condition dont just end with else if.
thanks