CSES - Shared codeLink to this code: https://cses.fi/paste/b4d685acb261aaca26c573/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class CountingRooms implements Runnable {
    public static void main(String arg[]) {
        new Thread(null, new CountingRooms(), "CountingRooms", 1<<28).start();
    }

    private static void dfs(boolean[][] visited, char[][] grid, int i, int j, int n, int m) {
        visited[i][j] = true;
        int dx[] = {-1,1,0,0};
        int dy[] = {0,0,-1,1};
        for(int d=0;d<4;d++) {
            if(isValid(i+dx[d], j+dy[d], n, m) && grid[i+dx[d]][j+dy[d]] == '.' && !visited[i+dx[d]][j+dy[d]]) {
                dfs(visited,grid,i+dx[d],j+dy[d],n, m);
            }
        }
    }

    private static boolean isValid(int i, int j, int n,int m) {
        return i>=0 && i<n && j>=0 && j<m;
    }

    @Override
    public void run() {
        FastScanner fs = new FastScanner();
        int n = fs.nextInt();
        int m = fs.nextInt();
        char[][] grid = new char[n][m];
        for(int i=0;i<n;i++) {
            String s = fs.next();
            if(s.length() != m) {
                throw new RuntimeException("INPUT MISMATCH");
            }
            grid[i] = s.toCharArray();
        }
        int cnt=0;
        boolean [][] visited = new boolean[n][m];
        for(int i=0;i<n;i++) {
            for(int j=0;j<m;j++) {
                if(!visited[i][j] && grid[i][j] == '.') {
                    dfs(visited,grid,i,j,n,m);
                    cnt++;
                }
            }
        }
        System.out.println(cnt);
    }

    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }
}