/** * Islands: Steven Zeil */ import java.awt.Point; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Islands_sjz { private BufferedReader input; public Islands_sjz(BufferedReader input) { this.input = input; } public static void main(String[] args) throws FileNotFoundException, IOException { if (args.length > 0) { for (int i = 0; i < args.length; ++i) { try (BufferedReader input = new BufferedReader(new FileReader(args[i]))) { new Islands_sjz(input).solve(); } } } else { BufferedReader input = new BufferedReader (new InputStreamReader(System.in)); new Islands_sjz(input).solve(); } } private void solve() throws IOException { String line = input.readLine(); Scanner numInput = new Scanner(line); int nRows = numInput.nextInt(); int nCols = numInput.nextInt(); String[] map = new String[nRows]; for (int i = 0; i < nRows; ++i) { map[i] = input.readLine(); } solve (nRows, nCols, map); numInput.close(); } private void solve(int nRows, int nCols, String[] map) { Queue q = new LinkedList(); int[][] landMasses = new int[nRows][nCols]; int counter = 0; for (int y = 0; y < nRows; ++y) { String row = map[y]; for (int x = 0; x < nCols; ++x) { landMasses[y][x] = 0; char c = row.charAt(x); if (c == 'L') { q.add(new Point(x,y)); ++counter; landMasses[y][x] = counter; } if (c == 'W') { landMasses[y][x] = Integer.MAX_VALUE; } } } while (!q.isEmpty()) { Point p = q.remove(); int massNumber = landMasses[p.y][p.x]; check(landMasses, nRows, nCols, massNumber, p.x+1, p.y, q); check(landMasses, nRows, nCols, massNumber, p.x-1, p.y, q); check(landMasses, nRows, nCols, massNumber, p.x, p.y+1, q); check(landMasses, nRows, nCols, massNumber, p.x, p.y-1, q); } boolean[] remaining = new boolean[counter+1]; Arrays.fill(remaining, false); counter = 0; for (int y = 0; y < nRows; ++y) { for (int x = 0; x < nCols; ++x) { int massNumber = landMasses[y][x]; if (massNumber > 0 && massNumber < Integer.MAX_VALUE && !remaining[massNumber]) { ++counter; remaining[massNumber] = true; } } } System.out.println(counter); } private void check(int[][] landMasses, int nRows, int nCols, int massNumber, int x, int y, Queue q) { if (x >= 0 && x < nCols && y >= 0 && y < nRows) { int mn = landMasses[y][x]; if (massNumber > mn) { landMasses[y][x] = massNumber; q.add(new Point(x,y)); } } } }