class
GfG
{
static
boolean
isZombie(
int
i,
int
j,
int
r,
int
c, String mat[])
{
if
(i <
0
|| j <
0
|| i >= r || j >= c
|| mat[i].charAt(j) !=
'Z'
)
return
false
;
return
true
;
}
static
int
Plant_Vs_Zombies(String mat[],
int
row,
int
col)
{
int
i, j, count =
0
;
for
(i =
0
; i < row; i++)
{
for
(j =
0
; j < col; j++)
{
if
(mat[i].charAt(j) ==
'P'
)
{
if
(!isZombie(i -
1
, j -
1
, row, col, mat)
&& !isZombie(i -
1
, j, row, col, mat)
&& !isZombie(i -
1
, j +
1
, row, col, mat)
&& !isZombie(i, j -
1
, row, col, mat)
&& !isZombie(i, j, row, col, mat)
&& !isZombie(i, j +
1
, row, col, mat)
&& !isZombie(i +
1
, j -
1
, row, col, mat)
&& !isZombie(i +
1
, j, row, col, mat)
&& !isZombie(i +
1
, j +
1
, row, col, mat)) {
count++;
}
}
}
}
return
count;
}
public
static
void
main(String []args)
{
String[] mat = {
"**P*"
,
"*Z**"
,
"*Z**"
,
"***P"
};
int
row = mat.length;
int
col = mat[
0
].length();
System.out.println(Plant_Vs_Zombies(mat, row, col));
}
}