using
System;
using
System.Collections.Generic;
class
Program
{
static
List<(
int
,
int
)> directions =
new
List<(
int
,
int
)>
{
(-1, 0), (1, 0), (0, -1), (0, 1)
};
static
void
Dfs(List<List<
char
>> worldMap,
int
row,
int
col,
int
rows,
int
cols)
{
worldMap[row][col] =
'W'
;
foreach
(
var
dir
in
directions)
{
int
newRow = (row + dir.Item1 + rows) % rows;
int
newCol = (col + dir.Item2 + cols) % cols;
if
(worldMap[newRow][newCol] ==
'L'
)
{
Dfs(worldMap, newRow, newCol, rows, cols);
}
}
}
static
int
CountIslandsOnMap(List<List<
char
>> worldMap)
{
int
rows = worldMap.Count;
int
cols = worldMap[0].Count;
int
islands = 0;
for
(
int
i = 0; i < rows; i++)
{
for
(
int
j = 0; j < cols; j++)
{
if
(worldMap[i][j] ==
'L'
)
{
islands++;
Dfs(worldMap, i, j, rows, cols);
}
}
}
return
islands;
}
static
void
Main()
{
List<List<
char
>> worldMap =
new
List<List<
char
>>
{
new
List<
char
>{
'L'
,
'W'
,
'W'
,
'L'
},
new
List<
char
>{
'L'
,
'W'
,
'W'
,
'L'
},
new
List<
char
>{
'L'
,
'W'
,
'W'
,
'L'
},
new
List<
char
>{
'L'
,
'W'
,
'W'
,
'L'
}
};
Console.WriteLine(CountIslandsOnMap(worldMap));
}
}