#include <iostream>
using namespace std;

int n, m;
char a[101][101];
bool visited[101][101];

int dx[8] = {-1,0,1,-1,1,-1,0,1};
int dy[8] = {-1,-1,-1,0,0,1,1,1};


void dfs(int r, int c) {
    visited[r][c] = true;

    for (int d = 0; d < 8; d++) {
        int nx = r + dx[d];
        int ny = c + dy[d];
        if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
        if (visited[nx][ny]) continue;
        if (a[nx][ny] != 'L') continue;

        dfs(nx, ny);   // 여기서 바로 다음 칸으로 "타고 들어감"
    }
}

int main() {
    cin >> m >> n;

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> a[i][j];

    int cnt = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (a[i][j] == 'L' && !visited[i][j]) {
                dfs(i, j);   // 새 호수 발견, 탐사 시작
                cnt++;
            }
        }
    }

    cout << cnt;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: