#!/bin/bash
export LC_ALL=C
read w h n
((--w));((--h))
rm -rf *.f
cat|eval tee '>(awk -vw=$w -vh=$h -v k='{0..7}\ \''
NR%8==k{
	s=$1<3?2:$1-1
	t=w<$1+1?w:$1+1
	u=$2<3?2:$2-1
	v=h<$2+1?h:$2+1
	for(i=s;i<=t;i++)for(j=u;j<=v;j++)print i,j|"sort -nk1,2 -o "k".f";echo -n
}
'\'\) \>/dev/null|cat
sort -mnk1,2 ?.f|uniq -c|sort -snk1|uniq -cw7|awk -vw=$w -vh=$h '{a[$2]=$1;a[0]=a[0]"-"$1}END{a[0]=--w"*"--h""a[0];for(i=0;i<=9;i++)printf"$((%s))\n",a[i]?a[i]:0|"eval echo $(cat)"}'|tr \  \\n