program paslaptingi_zemynai;
  { 44 u‘davinys }
  uses crt;
  const EIL = 16;
        ST = 48;
  type zemelapis = array [0..EIL + 1, 0..ST + 1] of char;

  procedure ivesti (var z : zemelapis);
    { veda ‘em‚lap }
    var f : text;
        i, j : integer;
  begin
    assign (f, 'ZEM.DAT');
    reset (f);
    for i := 1 to EIl do
      begin
        for j := 1 to ST do
          read (f, z[i, j]);
        readln (f);
      end;
    close (f);
  end;

  procedure tirti (z : zemelapis; var s, v : integer);
    { surenka statistinius duomenis apie pradin ‘em‚lap }
    var i, j : integer;
  begin
    s := 0;
    v := 0;
    for i := 1 to EIL do
      for j := 1 to ST do
        if z[i, j] = 'V'
           then v := v + 1
           else s := s + 1;
  end;

  procedure spausdinti (z : zemelapis; var f : text);
    { spausdina ‘em‚lap }
    var i, j : integer;
  begin
    for i := 1 to EIL do
      begin
        for j := 1 to ST do
          write (f, z[i, j]);
        writeln (f);
      end;
  end;

  procedure kalnas (var z : zemelapis);
    { suranda kalno langelius }
    var i, j : integer;
  begin
    for i := 1 to EIL do
      for j := 1 to ST do
        if (z[i, j] = 'S') or (z[i, j] = 'K')
           then if (z[i - 1, j] in ['S', 'K']) and
                   (z[i + 1, j] in ['S', 'K']) and
                   (z[i, j - 1] in ['S', 'K']) and
                   (z[i, j + 1] in ['S', 'K'])
                   then z[i, j] := 'K'
  end;

  procedure pusiasalis (var z : zemelapis;
                        var pakeista : boolean);
    { suranda pusiasalio langelius dabartiniame ‘em‚lapyje }
    procedure kas (lang : char; { nagrin‚jamas langelis }
                    var vt, pus : integer);
      { nustato, ar duotasis langelis yra vandens ar pusiasalio }
      { langelis }
    begin
      case lang of
       'V' : vt := vt + 1;
       'P' : pus := pus + 1;
      end;
    end; { kas }

    var i, j, vt, pus : integer;
  begin { pusiasalis }
    pakeista := false;
    for i := 1 to EIL do
      for j := 1 to ST do
        if z[i, j] = 'S'
           then begin
                  vt := 0;
                  pus := 0;
                  kas (z[i - 1, j], vt, pus);
                  kas (z[i + 1, j], vt, pus);
                  kas (z[i, j - 1], vt, pus);
                  kas (z[i, j + 1], vt, pus);
                  if (vt = 3) or (vt = 2) and (pus >= 1) or
                     (vt = 1) and (pus >= 2)
                   then begin
                          z[i, j] := 'P';
                          pakeista := true
                        end;
               end;
  end;

  procedure pakrante (var z : zemelapis);
    { suranda pakrant‚s langelius }
    var i, j : integer;
  begin
    for i := 1 to EIL do
      for j := 1 to ST do
        if z[i, j] = 'S'
           then z[i, j] := 'R'
  end;

  procedure jura (var z : zemelapis;
                        var pakeista : boolean);
    { suranda j–ros langelius dabartiniame ‘em‚lapyje }
    procedure kas (lang : char; { nagrin‚jamas langelis }
                    var o, ju, s : integer);
      { nustato, ar duotasis langelis yra okeano, j–ros ar }
      { sausumos langelis }
    begin
      case lang of
       'O' : o := o + 1;
       'J' : ju := ju + 1;
       'K', 'P', 'R' : s := s + 1
      end;
    end; { kas }

    var i, j, o, ju, s : integer;
  begin { jura }
    pakeista := false; { neradome nei vieno naujo j–ros langelio }
    for i := 1 to EIL do
      for j := 1 to ST do
        if z[i, j] = 'O'
           then begin
                  s := 0;
                  ju := 0;
                  o := 0;
                  kas (z[i - 1, j], o, ju, s);
                  kas (z[i + 1, j], o, ju, s);
                  kas (z[i, j - 1], o, ju, s);
                  kas (z[i, j + 1], o, ju, s);
                  if (ju >= 2) and (o <= 1) or (ju = 1) and (s >= 2) or
                     (s >= 2) and (o >= 1)
                   then begin
                          z[i, j] := 'J';
                          pakeista := true
                        end;
               end;
  end;
  procedure ezeras (var z : zemelapis);
    { suranda e‘ero langelius }
    var i, j : integer;
  begin
    for i := 1 to EIL do
      for j := 1 to ST do
        if z[i, j] = 'V'
           then z[i, j] := 'E'
  end;


  procedure zemynas (var z : zemelapis);
    { ižtiria ‘emyn… }

    procedure okeanas (i, j : integer);
    { suranda okeano srit, kuriai priklauso langelis (i, j) }
  begin
    z[i, j] := 'O';
    if (i >= 1) and (z[i - 1, j] = 'V')
       then okeanas (i - 1, j);
    if (i <= EIL) and (z[i + 1, j] = 'V')
       then okeanas (i + 1, j);
    if (j >= 1) and (z[i, j - 1] = 'V')
       then okeanas (i, j - 1);
    if (j <= ST) and (z[i, j + 1] = 'V')
       then okeanas (i, j + 1);
  end;

    var pakeista : boolean;
        i, j : integer;
  begin
    { ižtirsime sausumos tipo langelius }
    kalnas (z);
    pakeista := true;
    while pakeista do
      pusiasalis (z, pakeista);
    pakrante (z);
    { ižtirsime vandens tipo langelius }
    okeanas (0, 0);
    pakeista := true;
    while pakeista do
      jura (z, pakeista);
    ezeras (z)
  end;

  procedure statistika (z : zemelapis;
                        var  k, p, r, o, j, e : integer);
    { surenka statistik… apie ižtirt… ‘em‚lap }
    var i, ii : integer;
  begin
    k := 0; p := 0; r := 0;
    o := 0; j := 0; e := 0;
    for i := 1 to EIL do
      for ii := 1 to ST do
        case z[i, ii] of
         'K' : k := k + 1;
         'P' : p := p + 1;
         'R' : r := r + 1;
         'O' : o := o + 1;
         'J' : j := j + 1;
         'E' : e := e + 1;
        end;
  end;

  var z : zemelapis;
      i, s, v, k, p, r, o, j, e : integer;
      f : text;
begin
  { nuskaitomi pradiniai duomenys }
  ivesti (z);
  { ‘em‚lapio ižor‚ - okeanas }
  for i := 0 to EIL + 1 do
    begin
      z[i, 0] := 'V';
      z[i, ST + 1] := 'V';
    end;
  for i := 0 to ST + 1 do
    begin
      z[0, i] := 'V';
      z[EIL + 1, i] := 'V';
    end;
  { tiriamas pradinis ‘em‚lapis }
  tirti (z, s, v);
  { spausdinamas ‘em‚lapis }
  spausdinti (z, output);
  writeln ('Ne‘inoma: S=', s, ' V=', v, ' iž viso: ', v + s);
  readkey;
  { tiriami ‘emynai }
  zemynas (z);
  statistika (z, k, p, r, o, j, e);
  { ižtirtas ‘em‚lapis spausdinamas ekrane }
  spausdinti (z, output);
  write ('Ižtirta: P=', p, ' R=', r,  ' K=', k, ' O=', o);
  writeln (' J=', j, ' E=', e, ' iž viso: ', p + r + k + o + j + e);
  { ižtirtas ‘em‚lapis spausdinamas byloje }
  assign (f, '44_ZEMYN.OUT');
  rewrite (f);
  spausdinti (z, f);
  write (f, 'Ižtirta: P=', p, ' R=', r,  ' K=', k, ' O=', o);
  writeln (f, ' J=', j, ' E=', e, ' iž viso: ', p + r + k + o + j + e);
  close (f);
end.