import java.awt.Point;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Stack;


public class MineFinder {
	private static final int HEIGHT = 9;
	private static final int WIDTH = 12;
	
	public static final int CELL_UNEXPLORED = 0;
	public static final int CELL_MINE = 1;
	public static final int CELL_EXPLORED = 2;
	
	private int[][] grid = new int[WIDTH][HEIGHT];
	
	public static void main(String[] args) {
		MineFinder finder = new MineFinder("minefield.txt");
	}
	
	public MineFinder(String fileName) {
		try {
			BufferedReader in = new BufferedReader(new FileReader(fileName));
			
			String nextLine;
			int y = 0;
			while ((nextLine = in.readLine()) != null) {
				String[] parts = nextLine.split(" ");
				for (int x = 0; x < parts.length; x++) {
					grid[x][y] = Integer.parseInt(parts[x]);
				}
				y++;
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		findMines(new Point(0, 0));
	}
	
	public void findMines(Point start) {
		Stack<Point> stack = new Stack<Point>();
		stack.push(start);
		
		while (!stack.isEmpty()) {
			Point next = stack.pop();
			
			if (next.x < WIDTH && next.y < HEIGHT) {
				if (grid[next.x][next.y] == CELL_UNEXPLORED
						|| grid[next.x][next.y] == CELL_MINE) {
					if (grid[next.x][next.y] == CELL_MINE) {
						System.out.println("Mine found at (" + next.x + ", " + next.y + ")");
					}
					grid[next.x][next.y] = CELL_EXPLORED;
					stack.push(new Point(next.x, next.y + 1));
					stack.push(new Point(next.x + 1, next.y));
				} 
			}
		}
	}
}
 

