// 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 ColonyCountLite {
	
	// Slide
	private static boolean[][] slide;
	private static int slideLength, slideWidth; // For easier ref

	public static void main(String[] args) {
		
		try {
			Scanner input = new Scanner(new File("BacteriaSample.txt"));
			String bufferS;
			slideLength = input.nextInt()-1;
			slideWidth = input.nextInt()-1;
			slide = new boolean[slideLength+1][slideWidth+1];
			input.nextLine(); // Flush
			long timeStart = System.currentTimeMillis();
			int rowNum = 0;
			while (input.hasNextLine()){
				bufferS = input.nextLine();
				for (int i = 0; i < bufferS.length(); i++) {
					char c = bufferS.charAt(i);
					if (c=='*') slide[rowNum][i] = true;
					else slide[rowNum][i] = false;
				}
				rowNum++;
			}
			System.out.println(System.currentTimeMillis()-timeStart);
		} catch (FileNotFoundException e) {
			System.err.println("File \"" + "BacteriaSample.txt" + "\" not found. Now exiting...");
			System.exit(1);
		}
		long timeStart = System.currentTimeMillis();
		for (int x = 0; x < slide.length; x++) {
			for (int y = 0; y < slide[x].length; y++) {
				process(x,y);
			}
		}
		System.out.println(System.currentTimeMillis()-timeStart);
		
	} // main
	
	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

}
