using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("9 9");
WillReturn.Add(".........");
WillReturn.Add(".........");
WillReturn.Add(".........");
WillReturn.Add(".........");
WillReturn.Add("....#....");
WillReturn.Add(".........");
WillReturn.Add(".........");
WillReturn.Add(".........");
WillReturn.Add(".........");
//57
}
else if (InputPattern == "Input2") {
WillReturn.Add("2 2");
WillReturn.Add("..");
WillReturn.Add("..");
//0
}
else if (InputPattern == "Input3") {
WillReturn.Add("10 10");
WillReturn.Add("..........");
WillReturn.Add("....#.....");
WillReturn.Add("#.......#.");
WillReturn.Add("......#...");
WillReturn.Add(".......#..");
WillReturn.Add(".....#....");
WillReturn.Add("..........");
WillReturn.Add("..........");
WillReturn.Add("..#...#...");
WillReturn.Add(".......#..");
//64
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static long[] GetSplitArr(string pStr)
{
return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
}
struct PointDef
{
internal long X;
internal long Y;
}
static long UB_X;
static long UB_Y;
static char[,] mBanArr;
static long[,] mCntArr;
static void Main()
{
List<string> InputList = GetInputList();
mBanArr = CreateBanArr(InputList.Skip(1));
UB_X = mBanArr.GetUpperBound(0);
UB_Y = mBanArr.GetUpperBound(1);
mCntArr = new long[UB_X + 1, UB_Y + 1];
var KouhoQue = new Queue<PointDef>();
var BlackSet = new HashSet<long>();
for (long X = 0; X <= UB_X; X++) {
for (long Y = 0; Y <= UB_Y; Y++) {
if (mBanArr[X, Y] == '#') {
long Hash = GetHash(X, Y);
BlackSet.Add(Hash);
List<PointDef> KinbouPointList = DeriveKinbouPointList(X, Y);
foreach (PointDef EachKinbouPoint in KinbouPointList) {
KouhoQue.Enqueue(EachKinbouPoint);
mCntArr[EachKinbouPoint.X, EachKinbouPoint.Y]++;
}
}
}
}
if (BlackSet.Count == 0) {
Console.WriteLine(0);
return;
}
while (true) {
bool WillContinue = false;
// 一括操作なので、変更操作は遅延させる必要あり
var NewKouhoList = new List<PointDef>();
var LazyAddPointList = new List<PointDef>();
while (KouhoQue.Count > 0) {
PointDef EachKouho = KouhoQue.Dequeue();
long KouhoX = EachKouho.X;
long KouhoY = EachKouho.Y;
if (mCntArr[KouhoX, KouhoY] != 1) {
continue;
}
long KouhoHash = GetHash(KouhoX, KouhoY);
if (BlackSet.Add(KouhoHash) == false) continue;
WillContinue = true;
List<PointDef> KinbouPointList = DeriveKinbouPointList(KouhoX, KouhoY);
foreach (PointDef EachKinbouPoint in KinbouPointList) {
long KinbouHash = GetHash(EachKinbouPoint.X, EachKinbouPoint.Y);
if (BlackSet.Contains(KinbouHash)) continue;
LazyAddPointList.Add(EachKinbouPoint);
NewKouhoList.Add(EachKinbouPoint);
}
}
if (WillContinue) {
NewKouhoList.ForEach(pX => KouhoQue.Enqueue(pX));
LazyAddPointList.ForEach(pX => mCntArr[pX.X, pX.Y]++);
continue;
}
break;
}
Console.WriteLine(BlackSet.Count);
}
// 座標を引数として、4近傍の座標のListを返す
static List<PointDef> DeriveKinbouPointList(long pX, long pY)
{
var WillReturn = new List<PointDef>();
Action<long, long> AddAct = (pTargetX, pTargetY) =>
{
if (pTargetX < 0 || UB_X < pTargetX) return;
if (pTargetY < 0 || UB_Y < pTargetY) return;
PointDef WillAdd;
WillAdd.X = pTargetX;
WillAdd.Y = pTargetY;
WillReturn.Add(WillAdd);
};
AddAct(pX, pY - 1);
AddAct(pX, pY + 1);
AddAct(pX - 1, pY);
AddAct(pX + 1, pY);
return WillReturn;
}
// 座標のハッシュ値を返す
static long GetHash(long pX, long pY)
{
return pX * 1000000 + pY;
}
////////////////////////////////////////////////////////////////
// IEnumerable<string>をcharの2次元配列に設定
////////////////////////////////////////////////////////////////
static char[,] CreateBanArr(IEnumerable<string> pStrEnum)
{
var StrList = pStrEnum.ToList();
if (StrList.Count == 0) {
return new char[0, 0];
}
int UB_X = StrList[0].Length - 1;
int UB_Y = StrList.Count - 1;
char[,] WillReturn = new char[UB_X + 1, UB_Y + 1];
for (int Y = 0; Y <= UB_Y; Y++) {
for (int X = 0; X <= UB_X; X++) {
WillReturn[X, Y] = StrList[Y][X];
}
}
return WillReturn;
}
}