Problem:
Problem B: Red and Black

There is a rectangular room, covered with square tiles. Each tile is
colored either red or black. A man is standing on a black tile.
From a tile, he can move to one of four adjacent tiles. But he can't
move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach
by repeating the moves described above.

Input

The input consists of multiple data sets.
A data set starts with a line containing two positive integers W and H;
W and H are the numbers of tiles in the x-
and y- directions, respectively. W and H are
not more than 20.

There are H more lines in the data set, each of which
includes W characters. Each character represents the color of a
tile as follows.

'.' - a black tile

'#' - a red tile

'@' - a man on a black tile(appears exactly once in a data set)

The end of the input is indicated by a line consisting of two zeros.

Output

For each data set, your program
should output a line which contains the number of
tiles he can reach from the initial tile (including itself).

Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Output for the Sample Input

45
59
6
13

Solution in JAVA (judge verdict: Accepted)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

class Main{
	static int answer;
	static char tiles[][];
	static int w;
	static int h;

	public static void main(String args[]){
		// 標準入力準備
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		//Scanner sc = new Scanner(System.in);
		String str = new String();



		// 集合定義のためのハッシュ
		try{
			while(true){
				str = br.readLine();
				String strs[] = str.split(" ");
				w = Integer.parseInt(strs[0]);
				h = Integer.parseInt(strs[1]);

				if(w == 0) break;
				answer = 0;
				tiles = new char[h][w];

				int x = 0, y = 0;
				for(int i = 0; i < h; i++){
					//System.out.println("readLine " + i);
					str = br.readLine();
					for(int j = 0; j < w; j++){
						tiles[i][j] = str.charAt(j);
						if(tiles[i][j] == '@'){
							y = i;
							x = j;
						}
					}
				}

				df(x, y);
				System.out.println(answer);
			}
		}catch(Exception e){
			System.err.println(e);
		}
	}

	static void df(int x, int y){
		//System.out.println("位置: (" + x + "," + y + ")");
		tiles[y][x] = '#';
		answer++;

		// 上
		if(y > 0 && tiles[y-1][x] != '#')
			df(x, y-1);

		// 右
		if(x < w-1 && tiles[y][x+1] != '#')
			df(x+1, y);

		// 下
		if(y < h-1 && tiles[y+1][x] != '#')
			df(x, y+1);

		// 左
		if(x > 0 && tiles[y][x-1] != '#')
			df(x-1, y);

	}


	static void printArray(ArrayList<Integer[]> array){
		System.out.println("PrintArray " + array.size());
		for(int i = 0; i < array.size(); i++){
			Integer set[] = array.get(i);
			System.out.print("(" + set[0] + ", " + set[1] + ")");
		}
		System.out.println();
	}

	static void printMatrix(int matrix[][]){
		for(int row = 0; row < matrix.length; row++){
			for(int column = 0; column < matrix[row].length; column++){
				System.out.print(matrix[row][column] + "\t");
			}
			System.out.println();
		}
	}

	static void printMatrix(boolean matrix[][]){
		for(int row = 0; row < matrix.length; row++){
			for(int column = 0; column < matrix[row].length; column++){
				System.out.print(matrix[row][column] + "\t");
			}
			System.out.println();
		}
	}

	static void printMatrix(char matrix[][]){
		for(int row = 0; row < matrix.length; row++){
			for(int column = 0; column < matrix[row].length; column++){
				System.out.print(matrix[row][column]);
			}
			System.out.println();
		}
	}

	static void printArray(int array[]){
		for(int i = 0; i < array.length; i++){
			System.out.print(array[i] + "\t");
		}
		System.out.println();
	}
}

class OriginalSet{
	public ArrayList<Integer> set;

	OriginalSet(){
		set = new ArrayList<Integer>();
	}

	public void add(int inputInt){
		if(set.indexOf(inputInt) == -1){
			set.add(inputInt);
		}
	}

	public void addAll(OriginalSet inputSet){
		ArrayList<Integer> inputArray = inputSet.set;
		for(int i = 0; i < inputArray.size(); i++){
			add(inputArray.get(i));
		}
	}

	public void remove(int inputInt){
		set.remove((Integer)inputInt);
	}

	public void removeAll(OriginalSet inputSet){
		ArrayList<Integer> inputArray = inputSet.set;
		for(int i = 0; i < inputArray.size(); i++){
			remove(inputArray.get(i));
		}
	}

	public void print(){
		if(set.size() == 0){
			System.out.println("NULL");
			return;
		}
		// ソート
		Collections.sort(set);
		System.out.print(set.get(0));
		for(int i = 1; i < set.size(); i++){
			System.out.print(" " + set.get(i));
		}
		System.out.println();
	}
}