There is a standard way to deal with such situations - define an array with your valid moves and iterate over it. Here is an example how you can iterate over 8 neighboring cells:
static int moves[8][2] = {{-1,0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}};
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
for (int l = 0; l < 8 /*sizeof(move) / sizeof(move[0]) */; ++l) {
int ti = i + move[l][0];
int tj = j + move[l][1];
if (ti < 0 || ti >= WIDTH || tj < 0 || tj >= HEIGHT) {
continue;
}
// cell (ti, tj) is valid neighbor do something with it.
}
}
}