import java.util.*;
public class Main {
static int n, m;
static int[][] grid;
static boolean[][] vis;
static int[] dx = {1, -1, 0, 0};
static int[] dy = {0, 0, 1, -1};
static final int MAX = 1000000;
static final long MOD = 1000000007L;
static ArrayList<ArrayList<int[]>> components = new ArrayList<>();
static void bfs(int sx, int sy) {
Queue<int[]> q = new LinkedList<>();
ArrayList<int[]> component = new ArrayList<>();
q.offer(new int[]{sx, sy});
vis[sx][sy] = true;
while (!q.isEmpty()) {
int[] cur = q.poll();
int x = cur[0];
int y = cur[1];
component.add(new int[]{x, y});
for (int k = 0; k < 4; k++) {
int nx = x + dx[k];
int ny = y + dy[k];
if (nx >= 0 && nx < n && ny >= 0 && ny < m
&& grid[nx][ny] > 0 && !vis[nx][ny]) {
vis[nx][ny] = true;
q.offer(new int[]{nx, ny});
}
}
}
components.add(component);
}
public static void main
(String[] args
) {
Scanner sc
= new Scanner
(System.
in);
n = sc.nextInt();
m = sc.nextInt();
grid = new int[n][m];
vis = new boolean[n][m];
HashMap
<Integer, Integer
> freq
= new HashMap
<>();
// Read grid and build frequency map
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
grid[i][j] = sc.nextInt();
if (grid[i][j] > 0) {
freq.put(grid[i][j],
freq.getOrDefault(grid[i][j], 0) + 1);
}
}
}
// Find connected components
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] > 0 && !vis[i][j]) {
bfs(i, j);
}
}
}
long answer = 0;
// Process every component
for (ArrayList<int[]> component : components) {
// Remove current component
for (int[] cell : component) {
int value = grid[cell[0]][cell[1]];
freq.put(value, freq.get(value) - 1);
if (freq.get(value) == 0)
freq.remove(value);
}
// Calculate SED of every cell
for (int[] cell : component) {
int value = grid[cell[0]][cell[1]];
long sed = 0;
// Find unreachable multiples
for (int multiple = value; multiple <= MAX; multiple += value) {
if (freq.containsKey(multiple)) {
sed += (long) multiple * freq.get(multiple);
}
}
answer = (answer + sed) % MOD;
}
// Restore current component
for (int[] cell : component) {
int value = grid[cell[0]][cell[1]];
freq.put(value,
freq.getOrDefault(value, 0) + 1);
}
}
}
}