~cpp
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
/*
* Created on 2005. 1. 2
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class MineSweeper {
String [][] mineArr;
public int [] inputSize() {
String input = input();
String [] array = input.split(" ");
int len = array.length;
int [] intArray = new int[len];
for (int i = 0; i < len; i++)
intArray[i] = Integer.parseInt(array[i]);
return intArray;
}
public String[][] inputChar(int []array) {
int row = array[0];
int col = array[1];
mineArr = new String[row][col];
for(int i = 0; i < row; i++) {
String input = input();
String [] arr = input.split("");
for (int j = 0; j < col; j++)
mineArr[i][j] = arr[j+1];
}
return mineArr;
}
public String input() {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input = "";
try {
input = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return input;
}
public String[][] findPosition() {
int len = mineArr[0].length;
for (int row = 0; row < mineArr.length; row++) {
for (int col = 0; col < len; col++) {
if (mineArr[row][col].compareTo("*") == 0)
continue;
countMine(row, col);
}
}
return mineArr;
}
private void countMine(int row, int col) {
int count = 0;
int [] posRow = {-1, -1, 0, 1, 1, 1, 0, -1};
int [] posCol = {0, 1, 1, 1, 0, -1, -1, -1};
for (int i = 0; i < posRow.length; i++) {
if (row+posRow[i] >= 0 && col+posCol[i] >= 0 &&
row+posRow[i] < mineArr.length && col+posCol[i] < mineArr[0].length)
if (mineArr[row+posRow[i]][col+posCol[i]].compareTo("*") == 0)
count++;
}
mineArr[row][col] = Integer.toString(count);
}
private void printResult(Vector v) {
int size = v.size();
int i = 0;
while (i < size) {
String [][] arr = (String [][]) v.get(i);
int num = i+1;
System.out.println("Field #" + num + ":");
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[0].length; col++) {
System.out.print(arr[row][col]);
}
System.out.println();
}
System.out.println();
i++;
}
}
static public void main(String [] args) {
Vector v = new Vector();
while (true) {
MineSweeper m = new MineSweeper();
int [] array = m.inputSize();
if (array[0] == 0 && array[1] == 0) {
m.printResult(v);
break;
}
else {
m.inputChar(array);
m.findPosition();
v.add(m.mineArr);
}
}
}
}