// Package

// Imports
import java.io.*;
import java.util.*;

/**
 * Colony Count class
 * <p>
 * Counts colony numbers from "slide.dat" file.
 * <p>
 * For Mr. Benum, 
 * Comp Info Sci, ICS4M2
 * <p>
 * Licensed under <A HREF="http://www.gnu.org/licenses/gpl.txt">GNU GPL (Version 3)</A>. 
 * <p>
 * @author	Philip Peng
 * @version	1.0 Dec 10, 2007
 */

public class ColonyCount {
	
	// Slide
	private static boolean[][] slide;
	private static int slideLength, slideWidth; // For easier ref

	public static void main(String[] args) {
		
		// Slide data
		//File f = new File("slide.dat");
		File f = new File("SuperBigBacteriaSlide.txt");
		
		// Setup
		try {
			getData(f);
		} catch (FileNotFoundException e) {
			// No input data
			System.err.println("File \"" + f.getName() + "\" not found. Now exiting...");
			System.exit(1);
		}
			
		// Testing
		//printArray();
		
		
		// Timing
		long timeStart, timeEnd;
		timeStart = System.currentTimeMillis();
		
		// Process
		int buffer;
		for (int x = 0; x < slide.length; x++) {
			for (int y = 0; y < slide[x].length; y++) {
				buffer = process(x,y);
				/*
				if (buffer > 0) {
					System.out.println(
						"Colony of size " + buffer
						+ " found at " + "(" + x + "," + y + ")"
						); 
				}
				*/
			}
		}
		
		// Timing
		timeEnd = System.currentTimeMillis();
		System.out.println("The count took a total of " + (timeEnd-timeStart) + " milliseconds");
		
		// Done
		System.exit(0);
		
	} // main
	
	private static void getData(File f) throws FileNotFoundException {
		
		// Variables
		Scanner input;
		String buffer;
		
		// Setup
		input = new Scanner(f);
		
		// Create boolean array
		slideLength = input.nextInt()-1;
		slideWidth = input.nextInt()-1;
		slide = new boolean[slideLength+1][slideWidth+1];
		input.nextLine(); // Flush
		
		// Transfer data to array
		
		long timeStart, timeEnd;
		timeStart = System.currentTimeMillis();
		
		int rowNum = 0;
		while (input.hasNextLine()) {
			buffer = input.nextLine();
			for (int i = 0; i < buffer.length(); i++) {
				slide[rowNum][i] = bacteriaExist(buffer.charAt(i)); 
			}
			rowNum++;
		}
		
		timeEnd = System.currentTimeMillis();
		System.out.println("The count took a total of " + (timeEnd-timeStart) + " milliseconds");
		
		// Cleanup
		input.close();
		
	} // getData
	
	private static int process(int x, int y) {
		
		if (
			(x < 0)
			|| (y < 0)
			|| x > (slideLength)
			|| (y > (slideWidth))
			) { // Outside array
			return 0;
		} else if ( slide[x][y] == true ) { // Bacteria exists there
			slide[x][y] = false; // Toggle off
			return (
					1
					+ process(x, y+1)
					+ process(x, y-1)
					+ process(x+1, y)
					+ process(x-1, y)
					); // Check surroundings
		} else { // No bacteria there
			return 0;
		}
		
	} // process
	
	private static boolean bacteriaExist(char c) {
		
		// Return if slide location contains a bacteria or not 
		if (c=='-') return false;
		else if (c=='*') return true;
		else {
			System.err.println("Error processing character. Now exiting...");
			System.exit(1);
			return false; // Satisfy return
		}
		
	} // bacteriaExist
	
	private static void printArray() {
		
		// Testing
		for (int x = 0; x < slideLength+1; x++) {
			for (int y = 0; y < slideWidth+1; y++) {
				System.out.print(slide[x][y] + "\t");
			}
		}
		
	} // printArray

}
